Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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 主干:渲染前获取_Javascript_Mongodb_Backbone.js - Fatal编程技术网

Javascript 主干:渲染前获取

Javascript 主干:渲染前获取,javascript,mongodb,backbone.js,Javascript,Mongodb,Backbone.js,我正在尝试使用以下方法将新项目添加到我的主干收藏中: window.bearList.create({ name: "Tina" } ); 这将正确地将新项目保存到服务器,因为之后我可以在服务器上看到它,这正是我想要的。(我正在使用MongoDB) 我的bearList列表视图中有此绑定: initialize: function() { _.bindAll(this, 'render'); this.collection.bind('add', thi

我正在尝试使用以下方法将新项目添加到我的主干收藏中:

window.bearList.create({ name: "Tina" } );
这将正确地将新项目保存到服务器,因为之后我可以在服务器上看到它,这正是我想要的。(我正在使用MongoDB)

我的bearList列表视图中有此绑定:

    initialize: function() {
        _.bindAll(this, 'render');
        this.collection.bind('add', this.render);
    },
问题是,在我重新加载页面之前,上面的代码只是将以下内容添加到我的集合视图中

 {"name":"Tina"}
我尝试过使用model.save()回调,但仍然存在同样的问题

就像我说的,服务器上的一切看起来都很好,一旦我重新加载页面,集合中就有了“Tina”的更正版本

 {"name":"Tina"}
但由于某些原因,它无法获取ListView“render”事件的完整模型。我曾尝试在ListView渲染方法上分别获取每个模型,但这不起作用,而且是一种糟糕的做法

有人能帮我吗

以下是我的完整代码:

window.ListView = Backbone.View.extend({
    tagName: 'ul',
    className: 'list-group',

    initialize: function() {
        _.bindAll(this, 'render');
        this.collection.bind('add', this.render);
    },

    render: function(){
        this.$el.html(" ");
        this.collection.each(function(item){
        var listItemView = new ListItemView({ model: item });
        this.$el.append(listItemView.render().el);
        }, this);
    return this;
    },

});



window.ListItemView = Backbone.View.extend({
    tagName: 'li',
    className: 'list-group-item',

    initialize:function () {
        this.model.bind("change", this.render);
    },

    render:function () {
        console.log(JSON.stringify(this.model.toJSON()));
        this.$el.html("<a href='#"+ this.model.hashType + "/"+this.model.get('_id')+"' >" + this.model.get('name') + "</a>");
    return this;
    }
});
window.ListView=Backbone.View.extend({
标记名:“ul”,
类名:“列表组”,
初始化:函数(){
_.bindAll(这是“呈现”);
this.collection.bind('add',this.render));
},
render:function(){
这是。$el.html(“”);
此.集合.每个(功能(项){
var listItemView=new listItemView({model:item});
这是.el.append(listItemView.render().el);
},这个);
归还这个;
},
});
window.ListItemView=Backbone.View.extend({
标记名:“li”,
className:'列表组项',
初始化:函数(){
this.model.bind(“change”,this.render);
},
渲染:函数(){
log(JSON.stringify(this.model.toJSON());
这是。$el.html(“”);
归还这个;
}
});
只需通过等待:true

window.bearList.create({ name: "Tina" }, {wait: true} );

如果要在添加之前等待服务器,请传递{wait:true} 将新模型添加到集合中


收听
sync
事件,因为
add
只会添加您从主干网中创建的值

还有一个提示:使用listenTo可以使用主干网的更多功能。 而不是

initialize:function () {
    this.model.bind("change", this.render);
},
使用:


我确实看到了,它仍然没有在渲染功能中为我提供完整的模型。我只是得到了这样一个服务器响应:
{“name”:“Tina”,“message”:“Bear created!”}
听“sync”而不是add,还要确保服务器响应包含_id。根据主干文档:以及一个“sync”事件,一旦服务器成功创建了modelOk,谢谢。我只是修改了api以返回完整的JSON,而不是“Bear created”。这是有效的
initialize:function () {
    this.listenTo( this.model, "change", this.render );
},