2013-07-18 118 views
3

我阅读了关于这个参数的所有主题,但我不明白这段代码是什么,有几个小时我试着去感受一下它:Backbone.js - 必须指定一个“url”属性或函数

它说:“未捕获的错误:‘URL’属性或功能必须指定”当我触发事件保存并从TranslationView删除。 我试图找出其他代码,但即使加上明确的url属性设置为它不工作集合...谢谢你在前进

/** 
* Translation Collection - The document 
* -- Collection of all translations in a document 
*/ 
var Document = Backbone.Collection.extend({ 
     model: Translation, 
     localStorage: new Backbone.LocalStorage("translations-db")  
    }); 
var Docs = new Document; 

/** 
* Translation View 
* -- A single language version 
* This is a version of translation 
*/ 

var TranslationView = Backbone.View.extend({ 
    template: _.template('<div class="cnt-translation"><span class="delete-btn">delete</span><span class="save-btn">save</span> Language: <input value="english" /><textarea id="translation_0" class="translation"></textarea></div>'), 

    events: { 
     'click span.delete-btn': 'remove', 
     'click span.save-btn': 'save' 
    }, 
     //'chnage ul#main-menu #add': 'addText' 

    initialize: function(){ 
     _.bindAll(this, 'render', 'unrender', 'remove','save'); 
     this.listenTo(this.model, 'destroy', this.remove); 
    }, 

    render: function(counter){ 
     this.$el.html(this.template(this.model.toJSON()));  
     return this; 
    }, 

    unrender: function(){ 
     $(this.el).remove(); 
    }, 

    remove: function(){ 
     console.log(this.model); 
     this.model.destroy(); 
    }, 

    save: function(){ 
     console.log(this.model); 
     this.model.save(); 
     console.log(localStorage); 

    } 

}); 


/** 
* Translation Main View 
* -- The Application 
* This is the top level piece of the app 
*/ 

var AppView = Backbone.View.extend({ 
    el: $('#application'), 
    type: 'localStorage', // in future also "remoteStorage" 

    events: { 
     'click #add_trans': 'createOnEnter', 
     'click #save_trans': 'saveTranslations', 
     'click #remove_trans': 'removeTranslation' 
    }, 

    initialize: function(){ 
     _.bindAll(this, 
     'render', 
     'saveTranslations', 
     'addTranslation' 
    ); 
     this.listenTo(Docs, 'add', this.addTranslation); 
     this.listenTo(Docs, 'all', this.render); 
     this.listenTo(Docs, 'reset', this.reloadAll); 
     this.render(); 
     console.log('initialized and texts loaded'); 
     Docs.fetch(); 
    }, 
    .... 

    render: function(){ 
     var self = this; 
     /* 
     _(this.collection.models).each(function(translation){ 
     self.appendTranslation(translation); 
     }, this); 
     */ 
    } 

    addTranslation: function(){ 
     console.log('addTrans called'); 
     var translation = new Translation(); 
     translation.set({ 
     id: 'translation_' + Docs.length, 
     language: 'english' // modify item defaults 
     }); 
     var translationView = new TranslationView({ model: translation }); 
     $(this.el).append(translationView.render().el); 
     console.log(Docs); 
    }, 

    createOnEnter: function(e) { 
     Docs.create({title: 'new trans'}); 
    } 


}); 


var ENTER_KEY = 13;  
var app = new AppView(); 
console.log(app); 
})(jQuery); 
+0

这是太多的代码。我建议你调试一下,以缩小错误发生的位置。 –

+0

我删除了一些部分,但我认为它不能少,因为我不知道错误在哪里,我只知道它从删除或保存方法开始,并且可以找到错误在这里的一些地方..或者只是帮助我删除一些代码也许。你可以吗? – LowFieldTheory

+0

你的'Backbone collection'应该有一个'url:“/ translations”'。那不适合你? – NicoSantangelo

回答

2

你的问题是,您试图保存/摧毁一个模型对象这从未与您的本地存储支持收藏相关联。

本地存储插件首先查找模型上的localStorage财产,如果它发现没有它看起来对模型的收集工作localStorage若仍无localStorage发现回退为默认Backbone.Sync behaior这需要一个url等你拿例外。

而且因为你创造你有一个无助的模型对象的addTranslation

var translationView = new TranslationView({ model: translation }); 

但你并不需要这个,因为调用此方法,当一个项目添加到您的收藏,你会得到新添加项目作为参数。

你只需要改变你的方法使用该参数translation,而不是创建一个新的。

addTranslation: function(translation){ 
    translation.set({ 
     id: 'translation_' + Docs.length, 
     language: 'english' // modify item defaults 
    }); 

    var translationView = new TranslationView({ model: translation }); 

    $(this.el).append(translationView.render().el); 
}, 
+0

谢谢,正是这样!有一天,我需要深入挖掘骨干的来源。你知道一些文章详细介绍了骨干在里面的工作方式吗?我认为文档缺少一点.. – LowFieldTheory

+1

@Novalink我总是使用Backbone注释的源代码:http://backbonejs.org/docs/backbone.html – nemesv

相关问题