My issue is that I have a view and utils function that I am trying to connect with the model data.
In my view; I have a function:
getCalculatedData: function() {
var calculatedData = utils.calculateAmounts(_.clone(this.model.attributes))
},
This retrieves my model data with a key/value object. Now in my utils function I want to retrieve that data so I can use calculations. So I want to make a function that does this:
calculatedAmounts: function() {
//retrieve data from that
},
How do I retrieve those values. Lets say firstname, lastname, and state are all in that model that it is retrieving in my view. Do I create a variable hash that holds them like this:
calculatedAmounts: function() {
firstname : this.model.get('firstname');
//etc
},
How do I retrieve the values out of that object?
Thanks
I am not sure to completely understand your question, but in your
calculatedAmountsmethod, why don't you use the variable you passed when you called the method ingetCalculatedData?Your code would look like this:
Also, if you are going to modify your model in
calculatedAmountsand do not want those modifications to mirror outside thecalculatedAmountsscope, you should deep copy your model object.One common way to do it is to use the
extend()method of jQuery. It would look like:Edit:
Also, avoid passing
this.model.attributesto yourcalculatedAmounts()method if you want to use theget()method in it.this.model.attributesis anObjectobject, and not aBackbone.Modelobject.The
.get()you will call if you passthis.model.attributesas a param is the method part of the Object's prototype, and not the method part of theBackbone.Model's prototype.