0

In my app, I am fetching the data from /home -by home Model. the Model contains the defaults object. But while i fetch the data, I am not able to see the default object in the model.

here is my model :

define(['backbone'], function(Backbone){
    "use strict";
    socialApp = window.socialApp || {};

    socialApp.homeModel = Backbone.Model.extend({
        url: "/home",
        defaults:{
            "category":"Level-1"
        }
    });

    return socialApp.homeModel;
});

here is my view.js :

socialApp.homeView = Backbone.Marionette.ItemView.extend({
            tagName:'div',
            initialize:function () {
                var that = this;
                this.model.fetch().done(function(data){
                    that.render(data) // i am fetching here
                });
            },

            render: function (data) {
                console.log(data) //there is no defaults object here...
                this.$el.html(homeTemp(data));
            }
        });

What is wrong here? I am using Nodejs as a server.

here is the console what i am getting:

{
__v: 0
_id: "5416ce23fc0c41ec0f03f672"
email: "afzil@gmail.com"
firstName: "Mohamed"
lastName: "Afzil"
password: "afzil"
username: "afzil"
}

thanks in adavnce.

3gwebtrain
  • 14,640
  • 25
  • 121
  • 247
  • 1
    console log this.model and check – StateLess Sep 17 '14 at 05:33
  • 1
    is `this.model` in `socialApp.homeView` instance of `socialApp.homeModel` ? Plus there is no need to implement `render` in itemView and define `tagName:'div'` - its default. Its enough - `initialize:function () { this.model.on('fetch', this.render, this); this.model.fetch() }` – Evgeniy Sep 17 '14 at 06:25
  • @Evgeniy `Plus there is no need to implement render in itemView` - do you want me do remove the `render` method? is so how to pass the json to template? – 3gwebtrain Sep 17 '14 at 09:04
  • @3gwebtrain, yeap, you can remove it, Marionette has already implemented it for u ) – Evgeniy Sep 17 '14 at 09:34
  • And if you follow my second advice you can also get rid of that crap - `var that = this` – Evgeniy Sep 17 '14 at 09:35
  • yes I did, but I am getting result as `undefined`, I am using handlebar template plugin for requirejs! – 3gwebtrain Sep 17 '14 at 09:41
  • then better update/rewrite Marionette.Render to switch to handlebar than copy-past `render` in all views.. – Evgeniy Sep 17 '14 at 09:49
  • May be you are correct. But I am not able to understand. can you give / show me a sample or help me to do so? – 3gwebtrain Sep 17 '14 at 11:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61412/discussion-between-3gwebtrain-and-evgeniy). – 3gwebtrain Sep 17 '14 at 11:40

1 Answers1

1

As i can see in promise 'done' callback you have only fetch results, not model.

please modify your initialize function to this:

initialize: function () {
    var that = this;
    this.model.fetch({
        success: function(model){
            that.render(model.toJSON());
        }
    });
}
aleha_84
  • 8,309
  • 2
  • 38
  • 46