javascript - Backbone js and populating a model with data using fetch() - Stack Overflow

I'm working in Backbone js and I am trying to populate a model with data using fetch. The problem

I'm working in Backbone js and I am trying to populate a model with data using fetch. The problem is that the fetch appears to be working but my model is not populating with data.

A snippet of the code:

Backbone.emulateHTTP = true;
    Backbone.emulateJSON = true;

    ComponentsModel = Backbone.Model.extend({

        initialize : function() {

        },
        defaults : {
            ponent_id : null
        },
        urlRoot : "/ponents/ajax_ponent",

    });

    ComponentsView = Backbone.View.extend({
        el : $('body'),

        events : {
            'change #ponent-selector' : 'changeComponent',
        },

        initialize : function() {
            _.bindAll(this, 'render', 'changeComponent');
            this.render();
        },

        changeComponent : function(e) {
            var clickedEl = $(e.currentTarget);
            var value = clickedEl.attr("value");
            var ponent = new ComponentsModel({id :value, ponent_id :value });
            ponent.fetch();
            ponent.toJSON();
            alert(ponent.get('ponent_name'));

        },

        render : function() {

        },
    });

And the JSON being return from the server looks like this:

{"ponent_id":"1","ponent_name":"Test Component 1","ponent_description":"A simple test ponent","ponent_required":"","user_id":"1","ponent_approved":"0","ponent_price":"0","ponent_overview":"'"}

The alert is always undefined. Am I missing something?

I'm working in Backbone js and I am trying to populate a model with data using fetch. The problem is that the fetch appears to be working but my model is not populating with data.

A snippet of the code:

Backbone.emulateHTTP = true;
    Backbone.emulateJSON = true;

    ComponentsModel = Backbone.Model.extend({

        initialize : function() {

        },
        defaults : {
            ponent_id : null
        },
        urlRoot : "/ponents/ajax_ponent",

    });

    ComponentsView = Backbone.View.extend({
        el : $('body'),

        events : {
            'change #ponent-selector' : 'changeComponent',
        },

        initialize : function() {
            _.bindAll(this, 'render', 'changeComponent');
            this.render();
        },

        changeComponent : function(e) {
            var clickedEl = $(e.currentTarget);
            var value = clickedEl.attr("value");
            var ponent = new ComponentsModel({id :value, ponent_id :value });
            ponent.fetch();
            ponent.toJSON();
            alert(ponent.get('ponent_name'));

        },

        render : function() {

        },
    });

And the JSON being return from the server looks like this:

{"ponent_id":"1","ponent_name":"Test Component 1","ponent_description":"A simple test ponent","ponent_required":"","user_id":"1","ponent_approved":"0","ponent_price":"0","ponent_overview":"'"}

The alert is always undefined. Am I missing something?

Share Improve this question asked May 26, 2012 at 18:03 Devin DixonDevin Dixon 12.5k24 gold badges97 silver badges179 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Fetch is async, that's why it has success and error callbacks. So it's no sure that the data are fetched by the time you try to get the property. Try this:

ponent.fetch({
   success: function(){
       // Now you got your data
   }});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745617394a4636318.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信