javascript - How to set up a backbone model, and it's view without defining a collection? Currently have empty request p

I thought I had the Model, ModelView, Collection, AppView sample on githubdocumentcloudbackbone reaso

I thought I had the Model, ModelView, Collection, AppView sample on github/documentcloud/backbone reasonably understood, until..

I tried to set up something that is more cut-down. I'm trying to just have a Model and a View.

My issue

The post of data does not work; the request payload in the network trace is empty, what have I missed?

I have the plete code up on jsFiddle here (A). I also tested that the sample code that works in my enviroment also works in on jsFiddle (B) and network inspection shows that the request payload has data.

Model and View code

window.MyModel = Backbone.Model.extend({
    urlRoot: '/api/m',
});

window.MView = Backbone.View.extend({
    template: _.template($('#template').html()),
    el: $('#modelfields'),

    initialize: function () { this.model.bind('change', this.render, this); },

    events: {
        "click .save": function () { this.model.save(); }
    },
    render: function () { 
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    },
});

window.MyMView = new MView({model: new MyModel});

Network window screen shots

  • (A) Response data empty on my sample
  • (B) The official demo works fine

I thought I had the Model, ModelView, Collection, AppView sample on github/documentcloud/backbone reasonably understood, until..

I tried to set up something that is more cut-down. I'm trying to just have a Model and a View.

My issue

The post of data does not work; the request payload in the network trace is empty, what have I missed?

I have the plete code up on jsFiddle here (A). I also tested that the sample code that works in my enviroment also works in on jsFiddle (B) and network inspection shows that the request payload has data.

Model and View code

window.MyModel = Backbone.Model.extend({
    urlRoot: '/api/m',
});

window.MView = Backbone.View.extend({
    template: _.template($('#template').html()),
    el: $('#modelfields'),

    initialize: function () { this.model.bind('change', this.render, this); },

    events: {
        "click .save": function () { this.model.save(); }
    },
    render: function () { 
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    },
});

window.MyMView = new MView({model: new MyModel});

Network window screen shots

  • (A) Response data empty on my sample
  • (B) The official demo works fine
Share Improve this question asked Mar 7, 2012 at 6:45 Nick JosevskiNick Josevski 4,2264 gold badges45 silver badges64 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Your model has no properties! Set some defaults or call this.model.set({..}) to add them.

I've updated your example to include a default value and the fields from the form.

window.MyModel = Backbone.Model.extend({
    defaults: {
        'defaultvalue':'somedefault'                                     
    },
    urlRoot: '/api/m',
});

window.MView = Backbone.View.extend({
    template: _.template($('#template').html()),
    el: $('#modelfields'),

    initialize: function () { this.model.bind('change', this.render, this); },

    events: {
        "click .save": 'save'
    },
    save: function() {
        var description = this.$("#Description").val();
        var moreData = this.$("#MoreData").val();
        this.model.set({"description":description,"more":moreData});
        this.model.save();            
    },
    render: function () { 
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    },
});

window.MyMView = new MView({model: new MyModel});

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信