Reputation: 3223
I have a Backbone model named Summary which has attributes Total and DisplayTotal. What I would like to do is simply duplicate the value of Total and pass it to DisplayTotal.
Also is it possible to set a default for DisplayTotal?
defaults:{
"DisplayTotal" : this.Total,
}
If possible, as soon as the Total attribute is set, it should set its value to DisplayTotal too.
Upvotes: 0
Views: 39
Reputation: 38792
A few things are against this approach.
defaults
is interpreted in Class definition timeSo this
shouldn't be what you expect, probably this
would be window
or something like this.
Model.get()
functionSo your example is not correct it should be something like:
defaults:{
"DisplayTotal" : this.get( "Total" ),
}
But point P.1. is still making this to not work.
You have also a list of approaches to achieve want you want:
DisplayTotal
in Model.initialize()
initialize: function(){
this.set( "DisplayTotal", this.get( "Total" ) );
}
DisplayTotal
initialization to run timedefaults:{
"DisplayTotal" : function() { return this.get( "Total" ) }
}
Very danger solution, and probably you would have context problems with that this
.
DisplayTotal
anytime Total
is updatedinitialize: function(){
this.on( "change:Total", this.setDisplayTotal, this );
},
setDisplayTotal: function(){
this.set( "DisplayTotal", this.get( "Total" ) );
}
But I'm affraid you should combine this one with the S.1. due I don't think the event change
is triggered in the first set of the attributes.
Upvotes: 1