What's the best way to override Model.get(attr) in Backbone.js?

前端 未结 3 1176
野趣味
野趣味 2020-12-24 02:37

I\'m using Backbone.js for the first time, and liking it so far. One thing I can\'t work out at the moment in dynamic attributes of models. For example, say I have a Person

3条回答
  •  醉酒成梦
    2020-12-24 03:08

    I would think of attributes as the raw materials used by a model to provide answers to callers that ask the questions. I actually don't like having callers know too much about the internal attribute structure. Its an implementation detail. What if this structure changes?

    So my answer would be: don't do it.

    Create a method as you've done and hide the implementation details. Its much cleaner code and survives implementation changes.

提交回复
热议问题