创建馆藏后如何重新获取骨干馆藏? [英] How to re fetch backbone collection after collection create?
问题描述
有人可以告诉我在创建新模型时调用集合的create
函数后如何重新获取Backbone集合吗?
Can someone tell me how to re fetch a Backbone collection after calling collection's create
function when I create a new model?
在创建新模型后在集合上调用fetch
时,有时会得到该模型,有时却没有.
When I call fetch
on my collection after creating new model, sometimes I'm getting that model and sometimes not.
我的问题是,当我在集合中创建新模型时,我没有得到模型的ID,然后无法立即更新它,我需要刷新页面,然后得到ID.创建的模型.
My problem is when I create a new model in my collection, I'm not getting the id back of my model and then I can't update it immediately, I need to refresh the page and then I got the id of the created model.
我尝试使用listenTo
,但由于需要将更多集合发送到一个函数而无法使用.
I tried with listenTo
but I can't use it because I need to send more collections to one function.
关于我的引导程序模态的视图,保存时,我正在创建模型,该模型将持久存储到数据库中,并且在创建模型时,我会在控制台中获取除模型ID之外的所有属性.
And that my view for my bootstrap modal, on save I'm creating my model it persists to database and I'm getting all attributes in my console when I create it except models id.
主干视图:
app.types.EditView = Backbone.View.extend({
tagName: "div",
$container: $('#containerEdit'),
template: _.template($('#itemEdit-template').html()),
events:
{
"click .save": "save",
},
initialize: function(options)
{
this.options = options;
this.$container.html(this.render());
this.start();
this.end();
},
render: function()
{
this.$el.html(this.template());
return this.$el;
},
save: function()
{
console.log("save");
$('#openModal').modal('hide');
var dan = this.model.dan_u_tjednu_usera.datum;
var mjesec = this.model.dan_u_tjednu_usera.mjesecBrojevi;
var godina = this.model.dan_u_tjednu_usera.godina;
var start = $("#start").val();
var end = $("#end").val();
var user_id = this.model.user.id;
this.model.shifts.create({day: dan, month: mjesec, year: godina, time_from: start, time_to: end, user_id: user_id});
this.options.model.el.html($("<td href='#openModal' width='25%' align='center' class='list-group test' scope='row'>" + start + " - " + end + " " + "Admin" + "</td>"));
this.model.shifts.fetch({sync: true});
console.log("test", this.model.shifts);
}
在这里您可以看到,在我的回复中,我在创建时未获得id属性.
Here you can see that in my response im not getting the id attribute, on create.
在这里,您可以看到当我单击我的单元格时我记录了我的收藏集,而我这里没有所创建模型的id属性.而且我登录this.model时我也没有获得id属性
And here you can see when i click on my cell i log my collection and i have not the id attribute of the created model here. And im not getting the id attribute it too when i log this.model
推荐答案
主干的创建
方便在集合中创建模型的新实例. 等效于实例化带有属性哈希的模型,保存 将模型添加到服务器,然后将模型添加到集合中 成功创建.
Convenience to create a new instance of a model within a collection. Equivalent to instantiating a model with a hash of attributes, saving the model to the server, and adding the model to the set after being successfully created.
创建后无需获取集合,模型id
和任何其他字段都将自动合并到其attributes
哈希中.
There's no need to fetch a collection after a create, the model id
and any other field are automatically merged within its attributes
hash.
mikeapr4 没错,但他的示例可以得到改善.
While mikeapr4 is not wrong, his example could be improved.
{ wait: true }
是不必要的, 唯一的问题来自获取,而不是来自集合内部已经存在的模型.
The { wait: true }
is unnecessary if the only problem comes from the fetch, not from the model already being inside the collection.
此外,应避免使用once
,因为它是旧的"方式,而应使用listenToOnce
.请参见 ListenTo和on 之间的区别.
Also, once
should be avoided as it's the "old" way, and instead listenToOnce
should be used. See Difference between ListenTo and on.
如果您真的想在模型创建后就获取数据,那么在这里使用事件是过大的,相反,最好使用成功回调:
If you really want to fetch once a model is created, using events is overkill here, and instead, using the success callback is best:
save: function() {
// ..snip...
this.model.shifts.create({ /* ...snip... */ }, {
context: this,
success: this.onModelCreated
});
},
onModelCreated: function() {
// the model is now created and its attributes are up-to-date
this.model.shifts.fetch();
}
关于代码的其他说明
Backbone中没有sync
选项.只有 "sync"
事件和
Other notes on your code
There are no sync
option in Backbone. Only a "sync"
event and a sync
function.
避免使用全局jQuery选择器(例如$('.class-name')
),而是只要该元素在视图的元素内,就使用this.$('.class-name')
.
Avoid using the global jQuery selector (like $('.class-name')
) and instead, whenever the element is within the view's element, use this.$('.class-name')
.
此外,缓存jQuery元素以避免对find
方法进行昂贵的搜索.
Also, cache the jQuery element to avoid the costly search of the find
method.
可以像$("#start")
那样被缓存并重新使用.仅在重新渲染时重置缓存的元素.
Like $("#start")
could be cache and reused. Only reset the cached elements when re-rendering.
Backbone .render
函数应按约定返回this
.
The Backbone .render
function should return this
by convention.
然后,您的渲染调用应如下所示:
Then, your rendering call should look like:
this.$container.html(this.render().el); // el is enough
这篇关于创建馆藏后如何重新获取骨干馆藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!