Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone .fetch() not updating collection with the latest data
    primarykey
    data
    text
    <p>I am attempting to update a filtered <code>video</code> collection when a <code>user</code> model makes a request to the server. The problem is in the <code>update</code> method. <code>fetch()</code> does not get the latest data for the <code>video</code> collection.</p> <p>What do I need to fix in <code>update</code> to get the latest data so the view re-renders properly?</p> <pre><code>var VideosView = Backbone.View.extend({ tagName: 'ul', id: 'video-list', initialize: function() { var user = this.model; var _videos = user.get('videos'); user_vids = videos.filter_videos(_videos); this.listenTo(user, 'request', this.update); }, render: function() { user_vids.each( this.addOne, this ); return this; }, addOne: function(video) { var videoView = new VideoView({ model: video }); this.$el.append( videoView.render().el ); }, update: function() { $('#video-list').empty(); _videos = this.model.get('videos'); videos.fetch(); user_vids = videos.filter_videos(_videos) user_vids.each( this.addOne, this ); } }); // Instantiate var videosView = new VideosView({ model: user, collection: videos }); $('#allVideos').append( videosView.render().el ); </code></pre> <p><strong>EDIT</strong></p> <p>Adding additional code:</p> <p>Here is where <code>videosView</code> is initialized.</p> <pre><code>var IndexView = Backbone.View.extend({ initialize: function() { user = this.model; }, render: function() { var videosView = new VideosView({ model: user, collection: videos }); $('#allVideos').append( videosView.render().el ); var addVideoView = new AddVideoView({ model: user, collection: videos }); $('#addVideo').append( addVideoView.render().el ); } }); </code></pre> <p>The <code>listenTo</code> in <code>VideoViews</code> is listening for what is happening in <code>add_to_user_array</code> in the <code>AddVideoView</code> here:</p> <pre><code>var AddVideoView = Backbone.View.extend({ tagName: 'div', template: _.template( AddVideoTemplate ), events: { 'click #videoSubmitButton': 'submit' }, initialize: function() { user = this.model; }, render: function() { var template = this.template(); this.$el.html(template); return this; }, submit: function(event) { event.preventDefault(); console.log('form submitted'); var vimeo_id = parseInt( $('#vimeo_id').val() ); var newVideo = {vimeo_id: vimeo_id}; this.collection.create(newVideo, {wait: true}); this.add_to_user_array(vimeo_id); }, add_to_user_array: function(vimeo_id) { var _videos = user.get('videos'); _videos.push(vimeo_id); user.save({videos: _videos}, {wait: true}); } }); </code></pre> <p>Inside the router I'm instantiating the model and collection:</p> <pre><code>index: function() { users = new Users; users.fetch({ success: function(user_data) { var user = user_data.findWhere({username: App.username}) videos = new Videos; videos.fetch({ success: function(video_data) { var indexView = new IndexView({ model: user, collection: videos }); var indexController = new IndexController; indexController.showView( indexView ); } }); } }) } </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.
    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