2012-09-22 111 views
1

我開發一個骨幹網絡應用程序,我想知道,如何從視圖
發佈的數據這是我的模型:從視圖保存模型?

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

在你的路由器,你通過集合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}); 

初始化視圖,然後也相應地修改你的一些視圖代碼。