简体   繁体   English

如何将值从Collection收集到backbone.js中的模型

[英]how to Pass value From Collection to model in backbone.js

I want to pass and fetch a value from the view to model use of a collection, i am able to pass the value into the model when i used collection it is not working .i don't know where is the problem here is my code . 我想传递并从视图中获取一个值来模拟集合的使用,我能够将值传递给模型,当我使用集合时它不起作用。我不知道这里的问题在哪里就是我的代码。

my model 我的模特

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();
    }

});

my collection 我的收藏

var 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

});

here i am getting in console----> [],collection fetch value[object Object] ,where is the problem and how to save and fetch the value. 这里我进入控制台----> [],集合获取值[对象对象] ,问题在哪里以及如何保存和获取值。

Try this: 尝试这个:

var self = this;
collection.fetch()({
    success: function (data) {
        console.log("collection fetch value "+data);
        self.render();
    }
});

You want to only execute render once your fetch is successful. 您只想在获取成功后执行渲染。 The fetch method runs asynchronously. fetch方法以异步方式运行。 This means everything after it will still try to execute while the fetch method is still doing its thing. 这意味着在fetch方法仍在执行其操作时,它之后的所有内容仍将尝试执行。 By placing the render method in the success callback you ensure nothing tries to use your collection data until you actually have that data. 通过将render方法放在成功回调中,确保在实际拥有该数据之前不会尝试使用您的收集数据。

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

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