Backbone.js - custom setters

前端 未结 3 903
说谎
说谎 2021-02-19 08:44

Imagine a simple backbone model like

window.model= Backbone.Model.extend({
   defaults:{
      name: \"\",
      date: new Date().valueOf()
   }
})
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-19 09:15

    Not to toot my own horn, but I created a Backbone model with "Computed" properties to get around this. In other words

    var bm = Backbone.Model.extend({
      defaults: {
         fullName: function(){return this.firstName + " " + this.lastName},
         lowerCaseName: function(){
            //Should probably belong in the view
            return this.firstName.toLowerCase();  
    
         }
       }
    })
    

    You also listen for changes on computed properties and pretty much just treat this as a regular one.

    The plugin Bereznitskey mentioned is also a valid approach.

提交回复
热议问题