Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see a couple of issues with your example.</p> <ol> <li><p>menuView doesn't bind to any Coupon events, so if Coupon were to dispatch an event, menuView wouldn't know about it.</p></li> <li><p>You can specific a URL for your model and let Backbone get the data using fetch() rather than adding your own Ajax call to get data.</p> <pre><code>initialize: function () { this.coupon = new Coupon(); this.coupon.bind('change', this.couponCreated, this); this.coupon.fetch(); }, couponCreated: function () { Cart.add(this.coupon); } </code></pre></li> <li><p>It looks like you are making 3 ajax calls to get the same data. For example in menuView.couponReader() you do new Coupon() and Coupon.getCoupon() twice. Each one of these makes a new Ajax call the way you have it configured.</p></li> </ol> <p>It was hard to infer what you were trying to do in your example. It looks like you are trying to fetch a new Coupon on creation of the menuView and add it to the Cart. If that's the case, consider looking into the URL/fetch() method I was talking about earlier. You won't need to listen for events because you could handle it with a callback. In fact the problems you are having are likely asynchronous issues where you are adding the Coupon to the Cart before the Ajax call has come back with data.</p> <pre><code> couponReader: function () { var self = this , coupon = new Coupon(); coupon.fetch({success: function (model, response) { Cart.add(model); }); } </code></pre> <p>Alternatively, you can do a fetch() without a callback and listen for the 'change' event instead like I mentioned earlier in #2.</p> <p>Note: both of these examples rely on using Backbone's mechanism of synchronizing data using the Model's url property.</p>
    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.
 

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