Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting my model with Backbone.js
    primarykey
    data
    text
    <p>I'm attempting to use Backbone.js to simplify data (JSON) management and interaction with DOM.</p> <p>Firstly, I'm not sure if Backbone.js can indeed simplify and improve the current process, but I'd like to assume it can. </p> <p>Previously I'm retrieving the data with jQuery AJAX function. Now, I'm retrieving the data(still with AJAX) Backbone style into the Backbone model.</p> <p>For update, previously I was parsing through the JSON object itself to update data. I would then send back the updated json to the back-end (just as I've received it).</p> <p>Now, is it possible to use the set function in Backbone to simplify something like the below and <strong>ideally</strong> where should the set attribute behaviour (and all other UI bindings like change events) be constructed? Would it be on the fetch() success handler, which is in the View initializer?</p> <pre><code>function setBucketOffer(bucketName, newId) { var segments = json.segments; for (var i = 0; i &lt; segments.length; i++) { if (segments[i].market.toLowerCase() === g_market) { var genders = segments[i].gender; for (var i = 0; i &lt; genders.length; i++) { if (genders[i].name.toLowerCase() === g_segment) { var buckets = genders[i].buckets; for (var i = 0; i &lt; buckets.length; i++) { if (buckets[i].name === bucketName) { buckets[i].confirm = newId; return; } } } } } } } </code></pre> <p><strong>Example JSON</strong></p> <pre><code>{ "segments": [ { "market": "Market1", "gender": [ { "name": "male", "buckets": [ { "name": "Market1_M_CBD", "subscribers": "50,000", "postcode": "20000-2010", "lastsend": "13/03/12 4:30PM", "suggest": "10054", "confirm": "" }, { "name": "Market1_M_North", "subscribers": "50,000", "postcode": "20000-2010", "lastsend": "13/03/12 4:30PM", "suggest": "10054", "confirm": "" } ] }, { "name": "female", "buckets": [ { "name": "Market1_F_CBD", "subscribers": "50,000", "postcode": "20000-2010", "lastsend": "13/03/12 4:30PM", "suggest": "10054", "confirm": "10054" } ] } ] }, { "market": "Market2", "gender": [ { "name": "male", "buckets": [ { "name": "Market2_M_CBD", "subscribers": "50,000", "postcode": "20000-2010", "lastsend": "13/03/12 4:30PM", "suggest": "10054", "confirm": "10054" }, { "name": "Market2_M_North", "subscribers": "50,000", "postcode": "20000-2010", "lastsend": "13/03/12 4:30PM", "suggest": "10054", "confirm": "10054" }, { "name": "Market2_M_South", "subscribers": "50,000", "postcode": "20000-2010", "lastsend": "13/03/12 4:30PM", "suggest": "10054", "confirm": "10054" } ] } ] } ] } </code></pre> <p><strong>Edit 1</strong></p> <p>From here, I'm trying to make good use of Parse and to get just segments from my JSON:</p> <pre><code>var Offers = Backbone.Collection.extend({ url: 'URL', parse: function (response) { return response.segments; } }); </code></pre> <p>Here, I'm getting more than just response.segments. Also not sure if it's right for me to use the render function or fetch success function to populate the DOM. Suppose I have my html template in the DOM... I want to clone it using jQuery clone() and populate the clone using a forEach on segments, and push back all the clones into the html body. Is this workable in backbone, how would you do it? (I'm able to do this without backbone.js, but would like to see how I can improve with backbone.js, and bind all the data on the clones to model changes)</p> <pre><code>var OfferView = Backbone.View.extend({ initialize: function () { this.model = new Offers(); this.model.fetch({ success: function (collection, response) { console.log(response); } }); this.model.on('change', this.modelChange); this.model.on('change', this.render); this.modelChange = function () { alert('model changed'); }; }, render: function () { } }); </code></pre> <p><strong>Edit 2</strong></p> <p>I'm up to creating individual views through a forEach but am having trouble inserting these back into the DOM. What am I doing wrong? (Not sure around the return this part)</p> <pre><code>// DEFINE VIEW var OfferView = Backbone.View.extend({ initialize: function () { this.model = new Offers(); this.model.fetch(); this.model.on('change', this.modelChange); this.model.on('change', this.render); this.modelChange = function () { alert('model changed'); }; this.render(); }, render: function () { var self = this; this.model.forEach(function (s) { var view = new OfferMarketView({ id: "container" + s.get('name').toLowerCase().replace(/\s*/g, '') }); $('#leftCol').append(view.el); }); return this; } }); var OfferMarketView = Backbone.View.extend({ tagName: "div", className: "marketContainer", events: {}, render: function() { } }); </code></pre>
    singulars
    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.
 

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