Search code examples
backbone.js

Backbone attributes clone or pass in directly from this?


I need all of the attributes in one model. Is it considered good practice to give them as follows:

this.functionName(this.attributes);

from within the model, i am confused due to what it says in the backbone API under: http://backbonejs.org/#Model-attributes

it writes: "to retrieve and munge a copy of the model's attributes, use _.clone(model.attributes) instead." But I do not follow, should I clone them instead? and if so, why? I do not see any difference in the debugger between the cloned and this.attributes version

Thank you in advance


Solution

  • The keyword here is "munge".

    From wiki

    Mung or munge is computer jargon for a series of potentially destructive or irrevocable changes to a piece of data or a file.Common munging operations include removing punctuation or html tags, data parsing, filtering, and transformation.

    If you simply need a reference to the model attributes, what you're doing is fine, no need to overthink it.

    You should clone the attributes when you plan on changing the data and don't want to affect the original attributes.