KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Person = Backbone.Model.extend({ defaults: { name: 'Fetus', age: 0, children: [] }, initialize: function(){ alert("Welcome to this world"); }, adopt: function( newChildsName ){ var children_array = this.get("children"); children_array.push( newChildsName ); this.set({ children: children_array }); } }); var person = new Person({ name: "Thomas", age: 67, children: ['Ryan']}); person.adopt('John Resig'); var children = person.get("children"); // ['Ryan', 'John Resig'] In this example code we have: children_array = this.get("children") I was thinking this would just point to the same array in memory (and so would be O(1)). However then I thought that would be a design floor because one could manipulate the array without using this.set() and then event listeners wouldn't fire. So I'm guessing it (somehow magically) copies the array?? http://backbonejs.org/#Model-set What happens? edit: I just found the implementation in the backbone source code at https://github.com/documentcloud/backbone/blob/master/backbone.js (I've pasted relevant code at bottom) Get returns: return this.attributes[attr] so this would just point to the same array in memory right? So one could change the array without using set() and that would be bad.. ? am i correct? get: function(attr) { return this.attributes[attr]; }, // Get the HTML-escaped value of an attribute. escape: function(attr) { var html; if (html = this._escapedAttributes[attr]) return html; var val = this.get(attr); return this._escapedAttributes[attr] = _.escape(val == null ? '' : '' + va
Tags (comma-separated)
Save Edits
Cancel