从视图中保存模型?

时间:2012-09-22 17:41:33

标签: backbone.js save

我正在开发一个Backbone Web应用程序,我想知道如何从视图中发布数据 这是我的模特:

App.Models.edit = Backbone.Model.extend({
    defaults : {
        id    : undefined,
        fname : undefined,
        lname : undefined,
        phone : undefined,
        address : undefined,
        email : undefined,
        url: undefined,
    },

    initialize: function(){
        this.set({url : '../api/getOne/' + App.CurrentID });
    },

    getAttrs: function(attr){
        return this.get(attr);
    }
    });

这是我的观点:

App.Views.edit = Backbone.View.extend({
    el: $("#modal"),

    initialize: function(){
        App.TplNames.edit = $('body');
        App.Tpls.edit('edit');
        this.model.bind("reset", this.render, this);
        this.render();
    },

    events: {
        'click .btnSave': 'saveDetails',
    },

    saveDetails: function(){
        this.model.save();
        //console.log(this.model);
    },

    render: function(){
        var elem = '';
        $.each(this.model.models, function(i, k){
        var template = _.template( $('#tpl_edit').html(), k.toJSON() );
        elem += template;
        });
        $(this.el).html(elem);
        $("#myModal").modal('show');
        $("#myModal").on('hidden', function(){
        //alert(123);
        document.location.href = App.Url + '#view';
        });
        var attrs = "";
        $.each(this.model.models, function(i, k){
        attrs = k.toJSON();
        });
        $("#fname").val(attrs.fname);
        $("#lname").val(attrs.lname);
        $("#Email").val(attrs.email);
        $("#Phone").val(attrs.phone);
        $("#Address").val(attrs.address);
        //console.log(attrs);
    }
    });

这是我的路由器

App.Router = Backbone.Router.extend({
    routes: {
        ""      :   "def",
        "home"  :   "def",
        "view"  :   "getAll",
        "edit/:id"  :   "edit",
        "add"   :   "addContact",
    },

    def: function(){
        this.mainModel = new App.Collections.Main();
        this.mainView  = new App.Views.Main({model: this.mainModel});
        //this.mainModel.fetch();
    },

    edit: function(id){
        App.CurrentID = id;
        var contactCollObj = new App.Collections.edit();
        viewObj = new App.Views.edit({model: contactCollObj});
        contactCollObj.fetch();
        viewObj.render();
        //console.log(contactCollObj);
    },

    getAll: function(){
        //alert(123);
        var collObj = new App.Collections.View();
        var viewObj = new App.Views.View({model: collObj});
        collObj.fetch();
    },

    addContact: function(){
        //var model = new App.Collections.AddContact();
        model = new App.Models.AddContact();
        var view = new App.Views.AddContact({model: model});
        //view.render();
    }


    });

var app = new App.Router(); Backbone.history.start();

当我想保存模型时,它会产生错误:

this.model.save is not a function

除了以上所有事情都没关系......

1 个答案:

答案 0 :(得分:1)

在路由器中,您将集合作为模型传递给App.Collections.edit视图:

var contactCollObj = new App.Collections.edit();
viewObj = new App.Views.edit({model: contactCollObj});

这就是为什么你不能打电话给save()save()仅适用于非集合的模型。

您可能希望使用集合

初始化视图
viewObj = new App.Views.edit({collection: contactCollObj});

然后还相应地修改一些视图代码。

相关问题