Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 285
Sport Avatar asked Nov 11 '22 10:11

Sport


1 Answers

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. This means everything after it will still try to execute while the fetch method is still doing its thing. By placing the render method in the success callback you ensure nothing tries to use your collection data until you actually have that data.

like image 172
Katherine C Avatar answered Nov 14 '22 21:11

Katherine C