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

前端 未结 3 1169
野趣味
野趣味 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:22

    The actual properties used by Model.get are stored in the attribute property. You could do something like this:

    // function to cross-browser add a property to an object
    function addProperty(object, label, getter, setter) {
        if (object.defineProperty){
          object.defineProperty(object, label, {getter: getter, setter: setter})
        }
        else {
            object.__defineGetter__(label, getter)
            object.__defineSetter__(label, setter)
        }
    }
    
    // inside the initializer of your model, add a property to the attribute object
    var Person = Backbone.Model.extend({
        initialize: function(attr, options) {
            var t = this;
            ...
            addProperty(this.attributes, 'fullName',
                function() {return t.get('firstName') + ' ' + t.get('surname'),     
                function(val) {...}
            )
        }  
    })
    

    This will allow you to do person.get('fullName') as you requested.

    Edit: To be clear, I agree with Bill's answer below. Shouldn't really be dinking around with the internal implementation of backbone.js. Especially since this is incomplete...what about escape() instead of get()? And the setter is more complex, as it does validation, change notification, etc...now I'm sorry I posted this :)

提交回复
热议问题