jrsalunga
jrsalunga

Reputation: 419

BackboneJS: iterate on model attribute and change value

I want to make a function with functionality like toJSON()'s functionality which returns and edits model.

My question is how to iterate on model's attribute and edit the specific value of the attribute you selected.

If have a model e.g:

Item = Backbone.Model.extend({
    defaults: {
        name: '',
        amount: 0.00
    },
    toHTML: function(){
        // i think this is the place where
        // where i can do that function?

        //
        console.log(this.attribute)
    }
});
var item = new Item;

item.set({name: 'Pencil', amount: 5}): 

item.toJSON();
-> {name: 'Pencil', amount: 5}

// this is the function
item.toHTML();
-> {name: 'Pencil', amount: 5.00}

Upvotes: 1

Views: 5656

Answers (3)

Ashish Rana
Ashish Rana

Reputation: 85

Though the answers provided here are correct and would do what you want. But I think the better way would be to use the underscore functions for this purpose. for simple looping you can use

_.each(list, iteratee, [context])

_.each(model.attributes, function(item, index, items){
  console.log(item);
  console.log(index);
})

you can also use specialized functions as per your specific need. Like if you are want to have a new result array as a result of applying some function on every element of your list, map can be the best option for you.

_.map(list, iteratee, [context])

var newList = _.map(model.attributes, function(item, index, list){
  return item * 5;
})

I would recommend you to go through the documentation of underscore and backbone for the best function for your need.

Upvotes: 4

mu is too short
mu is too short

Reputation: 434606

You can iterate over an object using a for ... in loop and then use toFixed to format the number:

toHTML: function() {
    var attrs = { }, k;
    for(k in this.attributes) {
        attrs[k] = this.attributes[k];
        if(k === 'amount')
           attrs[k] = attrs[k].toFixed(2);
    }
    return attrs;
}

Note that amount will come out as a string but that's the only way to get 5.00 rather than 5 to come out. I'd probably leave the formatting up to the template and not bother with this toHTML implementation.

Demo: http://jsfiddle.net/ambiguous/ELTe5/

Upvotes: 5

Colin Brock
Colin Brock

Reputation: 21565

If you want to iterate over a model's attributes, use the attributes hash:

// Inside your model's method
for(attr in this.attributes){
    console.log(attr, this.attributes[attr]);
}

Here's a jsFiddle using your example code.

Upvotes: 4

Related Questions