Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干MVC视图没有';不同步更新_Javascript_Backbone.js_Underscore.js - Fatal编程技术网

Javascript 主干MVC视图没有';不同步更新

Javascript 主干MVC视图没有';不同步更新,javascript,backbone.js,underscore.js,Javascript,Backbone.js,Underscore.js,我一直在努力学习脊梁骨 我建立了一个模型集合,每个模型都有自己的视图,每次模型与服务器同步时都应该进行渲染。服务器发回模型的ID似乎在设置模型ID之前进行渲染。如果查看console.log()输出,我会让它在渲染之前打印模型的id。我还让它打印每个事件的发生。我得到: undefined (model id) add change:id change (not sure what changes here?) sync 另外,我让它在暂停2秒后更改模型的name属性model.save('n

我一直在努力学习脊梁骨

我建立了一个模型集合,每个模型都有自己的视图,每次模型与服务器同步时都应该进行渲染。服务器发回模型的ID似乎在设置模型ID之前进行渲染。如果查看console.log()输出,我会让它在渲染之前打印模型的id。我还让它打印每个事件的发生。我得到:

undefined (model id)
add
change:id
change (not sure what changes here?)
sync
另外,我让它在暂停2秒后更改模型的name属性
model.save('name','newname')
。这会导致另一个同步事件,但视图不会更新

以下是工作代码:

或者在这里看到:

var game_library;

(function($) {

    _.templateSettings = {
        interpolate: /\{(.+?)\}/g
    };    

    var GameModel = Backbone.Model.extend({
        url: '/gh/gist/response.json/2895708/',
        name: undefined,
        initialize: function() {

            // Create a view for this model
            this.view = new GameView({model: this});

            // Sync model with server
            this.save();

            // Bind events
            this.on('sync', this.view.render());
            this.on('all', this.console);
        },
        console: function(event, model, changes) {

            console.log('['+ ++this.i +']-------------(event[:attr], model, changes)----------------');
            console.log(event);
            console.log(model);
            console.log(changes);
        },
        i: 0
    });

    var GameCollection = Backbone.Collection.extend({
        model: GameModel
    });

    var GameView = Backbone.View.extend({
        el: $('#holder'),
        template: $('#game-template').html(),
        render: function() {

            var template = _.template(this.template);
            console.log(this.model.id);
            this.$el.html(template(this.model.toJSON()));
        }
    });

    // Instantiate new game collection
    game_library = new GameCollection;

    // Add a game
    // Note: can only pass in 1 ID from gist, so only add 1 game.
    var games = [
        new GameModel({name: 'Skyrim'})
    ];

    // Note: `game_library.add(new GameModel({name: 'Skyrim'}));` does
    // not work for some reason having to do with instances...
    game_library.add(games);

    // 2 sec later...
    window.setTimeout(function() {

        game_library.get(1).save('name', 'The Elder Scrolls V: Skyrim');
    }, 2000);
})( jQuery );
谢谢你的帮助!!​

拆下此处的支架:

// Bind events
this.on('sync', this.view.render/*()*/);
this.on('all', this.console);
拆下此处的支架:

// Bind events
this.on('sync', this.view.render/*()*/);
this.on('all', this.console);

更新要点见。我已将您的刷新调用移动到一个可以正常工作的本地函数。我不确定这背后的技术原因,但这是我以前使用它的方式。

更新了gist。我已将您的刷新调用移动到一个可以正常工作的本地函数。我不确定这背后的技术原因,但这是我以前使用它的方式。

谢谢!我想弄明白为什么它必须是本地的。也许这与
有关吧
?是的,我认为这与范围有关。事件处理方法在对象上注册。因此,需要在已注册的对象上访问回调。谢谢!我想弄明白为什么它必须是本地的。也许这与
有关吧
?是的,我认为这与范围有关。事件处理方法在对象上注册。因此,需要在注册的对象上访问回调。