javascript 如何建立一个主干模型,它的视图而不定义一个集合?当前有空的请求有效负载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9596950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:09:57  来源:igfitidea点击:

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

javascriptbackbone.js

提问by Nick Josevski

I thought I had the Model, ModelView, Collection, AppViewsample on github/documentcloud/backbonereasonably understood, until..

我以为我有模型模型视图收集APPVIEW在github / documentcloud /骨干样合理地理解,直到..

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

我试图建立一些更精简的东西。我试图只有一个Model和一个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 complete code upon 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.

我在jsFiddle here (A)上有完整的代码。我还测试了在我的环境中工作的示例代码也适用于jsFiddle (B)并且网络检查显示请求有效负载有数据。

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

网络窗口屏幕截图

采纳答案by tarn

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

您的模型没有属性!设置一些默认值或调用 this.model.set({..}) 来添加它们。

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