Bir koleksiyonun modelini modellemek için görünümden bir değer geçirmek ve almak istiyorum, koleksiyonun kullanılmadığı zaman değeri modele aktarabilirim Buradaki problemin nerede olduğunu bilmiyorum kodum.Nasıl Koleksiyondan Koleksiyona geçiş değeri backbone.js
modelim
var PostwallModel=Backbone.Model.extend({
urlRoot: 'http://localhost:3400/post',
idAttribute: '_id',
defaults : {
userId: '',
userName: " ",
postmsg : "unknown"
},
initialize: function() {
console.log("<><><>post model initialize<><><><><>");
},
// Delete item (row) from
clear: function() {
this.destroy();
}
});
koleksiyonum
burada konsolda alma öyleyimvar PostwallCollection = Backbone.Collection.extend({
url: 'http://localhost:3400/post',
model: PostwallModel
});
**here is my view**
var PostwallView = Backbone.View.extend({
el: $("#page"),
template: _.template(PostwallTemplate),
events: {
'click #postinwall' : 'submitpost',
},
initialize: function() {
console.log("_______postmodel");
this.model = new PostwallModel();
var obj= new PostwallModel();
obj.set({userId:'123',userName:"str ji",postmsg:'the post is here'});
console.log(obj.get('postmsg'));
obj.toJSON();
console.log(JSON.stringify(obj));
// console.log(obj.get('userName'));
var collection = new PostwallCollection();
_.bindAll(this, 'submitpost');
console.log(collection);
collection.add(obj,{id:1});
console.log("collection"+collection);
console.log("collection fetch value "+JSON.stringify(collection.fetch()));
this.render();
},
render: function() {
alert(" render function");
},
submitpost: function(e) {
//Save post model to server data
e.preventDefault();
var post_data = JSON.stringify(this.getFormData(this.$el.find('form')));
//
//this.model.save(post_data);
this.model.set(post_data);
this.collection.add(this.model);
return false
},
//Auxiliar function
//how to get data from textarea
});
----> [], koleksiyon getirme değeri [object Object], Problem nerede ve nasıl kaydedilir ve değer getirilir.
bunu biliyor muydunuz .fetch() senkronize işlem değil mi? console.log ("koleksiyon getirme değeri" + JSON.stringify (collection.fetch())); işe yaramaz. Daha fazla yardım için jsbin örneğini verebilir misin? –
ben noktanızı alamadım lütfen biraz daha bilgi verebilirsiniz @VasilVanchuk – Sport
Ne demek istediğim konu: collection.fetch() toplama verisini iade etmiyor, getirme işlemi http isteklerine ve verilere erişilebiliyor başarılı işlem yanıtı. Bu yüzden, eğer config.log ("toplama getirisi değeri" + JSON.stringify (collection.fetch())) gibi yazdığınızda; koleksiyon verilerini göremezsiniz –