Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone: (Callback) function even not binding to Collection
    primarykey
    data
    text
    <p>I want updatePlays function to be called as a callback function when an AJAX call is successful. I thought that using underscore bind would let me refer 'this' as the Collection object that I actually want to update, but I'm having trouble here. When I get to the function that should update collection, it thinks that 'this' refers to 'window'. </p> <p>In this situation, a Backbone Model has Backbone Collection, which are made from another backbone models. </p> <p>in view: </p> <pre><code>SomeView: Backbone.View.extend({ someFunction: function(e) { var field = this this.picker = new PlayPicker({ field:field, model: new PlaySet({ plays: new Collections.Plays({}), slot: field.model }) }) } }) PlayPicker:Backbone.View.extend({ ... refresh: function () { this.model.update() }, .... </code></pre> <p>Collection that's part of model PlaySet</p> <pre><code>Plays:Backbone.Collection.extend({ model: Play , initialize: function () { plays = this _.bind(this.updatePlays, plays) // Where I thought I should bind }, updatePlays: function (plays) { new_plays = [] var i; for (i = 0; i &lt; plays.length; i++){ new_plays.push(new Play({ id: plays[i]["id"], text: plays[i]["text"] })); } this.reset(new_plays) // Uncaught TypeError: Object [object Window] has no method 'reset' } }) </code></pre> <p>Model PlaySet</p> <pre><code>PlaySet: Backbone.Model.extend({ update: function() { this.get('slot').fetchAssociatedPlays(this.get('plays').updatePlays) }, }) </code></pre> <p>Model Slot - does the AJAX call</p> <pre><code>Slot:Backbone.Model.extend({ ... fetchAssociatedPlays: function(update) { thisModel = this $.ajax({ url: thisModel.associatedPlaysURL(), success: function (collection) { update(collection) } }) }}) </code></pre> <p>Should this be achievable with underscore bind, and where/how would be the correct way? </p> <p>Thank you in advance.</p>
    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.
    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