简体   繁体   English

首次触发事件后,主干视图不会更新

[英]Backbone view doesn't update after event fires for the first time

I have a backbone view that has a single click event to update a collection. 我有一个骨干视图,该视图具有单击事件以更新集合。 In my console, I can see the object being updated and the number of models being returned is changing, however the view stays static after the event is fired for the first time and any second attempt to fire it gives me the error TypeError: text is undefined . 在我的控制台中,我可以看到该对象正在更新并且返回的模型数量正在更改,但是在第一次触发该事件之后,该视图保持静态,并且任何第二次尝试触发它都会给我带来TypeError: text is undefined错误TypeError: text is undefined For reference, I have my script loading at the bottom of the page and the template (using underscore) is above it. 作为参考,我在页面底部加载了脚本,模板(使用下划线)位于页面上方。

Here's my view 这是我的看法

app.MyView = Backbone.View.extend({
el: 'body',
events: {
    'click #submit': 'fetchData'
},
initialize: function() {

    this.collection = new app.MyCollection();

    // On my first attempt, I tried having a the render tied to the sync event when the view initiated, although the sync event doesn't actually occur until something is clicked
    // this.collection.on('sync', this.render, this);
},
render: function() {
    console.log('rendering');

    var schedule = $('#schedule');
    var template = _.template($('#times-template').html());
    schedule.html(template({ collection: this.collection.toJSON() }));
},
fetchData: function() {

    that = this;

    stationQuery.station_start      = $('#start').val();
    stationQuery.station_end        = $('#end').val();

    var query = stationQuery;

    this.collection.fetch({
        data: query,
        success: function(collection, response) {
            console.log('fetching data');
            console.log(collection);

            // attempt #2 - moving the sync event to the success callback of fetch doesnt allow the view to update a second time either
            // collection.on('sync', that.render, that);

        },
        error: function(collection, response) {
            console.log('error on fetch', response);
        }
    });
},
});

app.myView = new app.MyView;

// Third attempt, this time checking if listenTo will allow the view to update every time the event is fired. It still only works on the first time and fails to render for consecutive clicks, even though the console is showing updated models
app.myView.listenTo(app.myView.collection, 'sync', app.myView.render);

Below is a working code, I just added initial call to fetch data at the end 下面是一个工作代码,我刚刚添加了初始调用以最后获取数据

app.myView.fetchData();

And uncommented your .on('sync', ... inside initialize 和注释去掉,你的.on('sync', ...里面initialize

this.collection.on('sync', this.render, this);

I tested it with jsbin so you can see what's was wrong for you. 我用jsbin进行了测试,这样您就可以了解问题所在。 As a result I see initial rendering of fetched data and clicking the #submit will re-fetch and re-render view. 结果,我看到了获取数据的初始呈现,然后单击#submit将重新获取并重新呈现视图。

app.MyView = Backbone.View.extend({
el: 'body',
events: {
    'click #submit': 'fetchData'
},
initialize: function() {

    this.collection = new app.MyCollection();

    // Here is a binding to each fetch data and on success render view
    this.collection.on('sync', this.render, this);
},
render: function() {
    console.log('rendering');

    var schedule = $('#schedule');
    var template = _.template($('#times-template').html());
    schedule.html(template({ collection: this.collection.toJSON() }));
},
fetchData: function() {

    that = this;
    var stationQuery = {};
    stationQuery.station_start      = $('#start').val();
    stationQuery.station_end        = $('#end').val();

    var query = stationQuery;

    this.collection.fetch({
        data: query,
        success: function(collection, response) {
            console.log('fetching data');
        },
        error: function(collection, response) {
            console.log('error on fetch', response);
        }
    });
},
});

app.myView = new app.MyView;

// Here is first call to fetch data and on success render view
app.myView.fetchData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM