骨干localstorage未定义

时间:2013-02-20 05:48:08

标签: javascript backbone.js local-storage

我正在尝试使用backbone.js&写一个简单的RSS。 localStorage的。我无法将表单值保存到localstorage。

我正在使用this& this作为我的参考。

在保存数据时,我收到以下错误

Resource failed to load 
    b.ajaxTransport.send jquery-1.9.1.min.js:5
    b.extend.ajax jquery-1.9.1.min.js:5
    g.ajax backbone-min.js:41
    g.sync backbone-min.js:41
    Backbone.sync backbone.localStorage.js:190
    f.extend.sync backbone-min.js:12
    f.extend.save backbone-min.js:16
    Backbone.View.extend.saveSource app.js:47
    b.event.dispatch jquery-1.9.1.min.js:3
    b.event.add.v.handle jquery-1.9.1.min.js:3

查看

var sourceView = Backbone.View.extend({
   tagName:"li",

   template:_.template($('#tmpl_sourceDetails').html()), 

   initialize: function () {
        console.log(this.model);
        this.model.bind("change", this.render, this);
    },

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

    events: {
            "change input": "change",
            "click .save": "saveSource",
            "click .delete": "deleteSource"
    },

    change: function (event) {
        var target = event.target;
        console.log('changing ' + target.id + ' from: ' + target.defaultValue + ' to: ' + target.value);

    },

    saveSource: function () {
        console.log(this.model);
        if (this.model.isNew()) {
            var self = this;
            site = $('#name').val();
            siteURL = $('#sourceURL').val(); 
        this.model.save({source:site,sourceURL:siteURL});
        this.$el.html(this.template(this.model.toJSON()));
        } else {
            this.model.save();
        console.log(this.Collections.length);   
        }
        return false;
       },

    deleteSource: function () {
        this.model.destroy({
            success: function () {
                alert('RSS source deleted successfully');
                window.history.back();
            }
        });
        return false;
    },

    close: function () {
        $(this.el).unbind();
        $(this.el).empty();
    }
});

COLLECTION

//indicates collection
//localstorage table defined here
var RSSCollection = Backbone.Collection.extend({
    model: RSS,
    url: "../api/RSS",
    localStorage: new Store("tblSource")// saving all items under tblSource namespace.

});

更新 我认为错误是因为model.localStorage未定义。在保存而不是调用backbone.localSync时,它调用Backbone.ajaxSync

backbone.localStorage.js 
    Backbone.getSyncMethod = function(model) {
      if(model.localStorage || (model.collection && model.collection.localStorage)) {
        return Backbone.localSync;
      }

      return Backbone.ajaxSync;
    };

我出错的任何想法/建议? 感谢

0 个答案:

没有答案