Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone FETCH from JSON, edit model data then SAVE back to JSON
    primarykey
    data
    text
    <p>I've been working through Code School's Anatomy of Backbone.js course, but am confused when trying to save model changes back to the server. Perhaps you can help.</p> <p>This is what I understand needs to happen:</p> <ol> <li>Populate collection from a JSON data source using fetch();</li> <li>Append the collection to the DOM</li> <li>Edit a model (uncheck checkbox, which sets 'favourite' to false)</li> <li>Save the model.</li> </ol> <p>My assumption is that if I were to unselect a record as a 'favourite' then hit refresh, the change would be persistant and also evident in the JSON file. However, this isn't the case and the original collection is loaded and JSON is unchanged.</p> <p>I think my confusion is in using the fetch method and declaring the URL within the model and collection.</p> <p><em><strong>How can I get this model change to be persistant?</em></strong></p> <p><strong>Model:</strong></p> <pre><code>var Contact = Backbone.Model.extend({ url: '/contacts', defaults:{ favourite: false }, toggleFavourite: function(){ if(this.get('favourite') === false) { this.set({ 'favourite': true }); } else { this.set({ 'favourite': false }) } this.save(); } }); </code></pre> <p><strong>Collection</strong></p> <pre><code>var Contacts = Backbone.Collection.extend({ model: Contact, url: '/contacts' }); </code></pre> <p><strong>Views</strong></p> <pre><code>var ContactView = Backbone.View.extend({ className: 'record', template: _.template('&lt;span&gt;&lt;%= name %&gt;&lt;/span&gt;' + '&lt;span class="phone-number"&gt;&lt;%= phone %&gt;&lt;/span&gt;' + '&lt;input type="checkbox" &lt;% if(favourite === true) print("checked") %&gt;/&gt;'), events: { 'change input': 'toggleFavourite', 'click .phone-number': 'dial' }, initialize: function(){ this.model.on('change', this.render, this); }, toggleFavourite: function(e){ this.model.toggleFavourite(); }, dial: function(e){ alert('Dialing now...'); }, render: function(){ this.$el.html(this.template(this.model.toJSON())); return this; } }); var ContactsView = Backbone.View.extend({ initialize: function(){ this.collection.on('add', this.addOne, this); this.collection.on('reset', this.addAll, this); }, addOne: function(contact){ var contactView = new ContactView({ model: contact }); this.$el.append(contactView.render().el); }, addAll: function(){ this.collection.forEach(this.addOne, this); }, render: function(){ this.addAll(); } }); </code></pre> <p><strong>App.js</strong></p> <pre><code>var contacts = new Contacts(); //creates list contactsView = new ContactsView({ collection: contacts}); //creates list view contacts.fetch({url: 'contacts/data.json'}); //populates list $('#mainPanel').append(contactsView.el); //appends list to DOM </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload