Computed properties in Backbone

后端 未结 4 1186
暖寄归人
暖寄归人 2020-12-03 05:25

I have a scenario where the data being manipulated on the client is presented and interacted with in a different way than it is represented on the server.

Consider t

4条回答
  •  无人及你
    2020-12-03 06:04

    We are very used to send model.toJSON() to feed the template. And this method is very tricky to overwrite because is used by other components.

    But we can feed the template with a custom model.toJSONDecorated() method that can look like this:

    # in your Backbone.Model
    toJSONDecorated: function(){
      return 
        _.extend( 
          this.toJSON(), 
          {
            start_date : Utils.dateFromDate( this.get( "start_at" ) ),
            start_time : Utils.timeFromDate( this.get( "start_at" ) ),
            duration   : Utils.youGetTheIdea( :) )
          } 
        );
    }
    

    Of course this is breaking a few patterns, I can live with it, if you don't you can move this logic to a Decorator class as people have suggested in other answers.

提交回复
热议问题