Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干本地存储不是';不要储存任何东西_Javascript_Backbone.js_Local Storage - Fatal编程技术网

Javascript 主干本地存储不是';不要储存任何东西

Javascript 主干本地存储不是';不要储存任何东西,javascript,backbone.js,local-storage,Javascript,Backbone.js,Local Storage,我正在尝试使用backone.localStorage将模型存储到本地存储中。不幸的是,没有存储任何内容 据我所知,收集只需要一行 localStorage: new Backbone.LocalStorage("days") 但那没用。如果我使用Chrome开发工具检查localStorage,则不会存储任何内容 这是我的代码: // Filename: app.js define([ 'jquery', 'underscore', 'backbone', '

我正在尝试使用backone.localStorage将模型存储到本地存储中。不幸的是,没有存储任何内容

据我所知,收集只需要一行

localStorage: new Backbone.LocalStorage("days")
但那没用。如果我使用Chrome开发工具检查localStorage,则不会存储任何内容

这是我的代码:

// Filename: app.js
define([
    'jquery',
    'underscore',
    'backbone',
    'localStorage'
], function($, _, Backbone){
    var initialize = function(){
        (function ($) {

            var Day = Backbone.Model.extend({
                initialize: function() {
                    this.on("change", this.calculateWorkTime);
                    this.calculateWorkTime();
                },
                defaults: {
                    "date":     new Date(),
                    "fromAm":   0,
                    "toAm":     0,
                    "fromPm":   0,
                    "toPm":     0,
                    "workTime": 0
                },
                isValidTime: function(n) {
                    return !isNaN(parseFloat(n)) && isFinite(n) && parseFloat(n)>0;
                },
                calculateWorkTime: function() {
                    var work = 0.0;
                    if (timeToFloat(this.get("fromAm")) < timeToFloat(this.get("toAm")) ) {
                        work += timeToFloat(this.get("toAm")) - timeToFloat(this.get("fromAm"));
                    }
                    if (timeToFloat(this.get("fromPm")) < timeToFloat(this.get("toPm")) ) {
                        work += timeToFloat(this.get("toPm")) - timeToFloat(this.get("fromPm"));
                    }
                    this.set("workTime", floatToTime(work));
                }
            });

            var DayList = Backbone.Collection.extend({
                model: Day,
                localStorage: new Backbone.LocalStorage("days")
            });

            var DataRow = Backbone.View.extend({
                tagName:  "tr",
                template: _.template($('#table-row-day').html()),
                events: {
                    "change .edit-field":  "updateModel",
                    "click #button-delete-day": "deleteDay"
                },
                initialize: function() {
                    this.listenTo(this.model, 'destroy', this.remove);
                },
                render: function() {
                    this.$el.html(this.template(this.model.toJSON()));
                    return this;
                },
                updateModel: function(event) {
                    var myValue = "leer";
                    switch (event.currentTarget.id) {
                        case "table-edit-date":
                            myValue = this.$('#table-edit-date').val();
                            this.model.set("date", myValue);
                            break;
                        case "table-edit-fromAm":
                            myValue = this.$('#table-edit-fromAm').val();
                            this.model.set("fromAm", myValue);
                            break;
                        case "table-edit-toAm":
                            myValue = this.$('#table-edit-toAm').val();
                            this.model.set("toAm", myValue);
                            break;
                        case "table-edit-fromPm":
                            myValue = this.$('#table-edit-fromPm').val();
                            this.model.set("fromPm", myValue);
                            break;
                        case "table-edit-toPm":
                            myValue = this.$('#table-edit-toPm').val();
                            this.model.set("toPm", myValue);
                            break;
                    }
                    this.render();
                    console.log(this.model.toJSON());
                },
                deleteDay: function(a, b, c, d) {
                    this.model.destroy();
                }
            });

            var Days = new DayList;

            AppView = Backbone.View.extend({
                el: $("body"),
                initialize: function () {
                    this.listenTo(Days, 'add', this.addOne);
                    this.listenTo(Days, 'all', this.refreshTotal);
                    this.total = this.$("#total-work");
                    Days.fetch();
                    this.refreshTotal();
                },
                events: {
                    "click #button-add-day":  "addDay"
                },
                addDay: function () {
                    var day = new Day({
                        "date":     $("#form-add-date").val(),
                        "fromAm":   $("#form-add-fromAm").val(),
                        "toAm":     $("#form-add-toAm").val(),
                        "fromPm":   $("#form-add-fromPm").val(),
                        "toPm":     $("#form-add-toPm").val()
                    });
                    Days.add(day);
                },
                addOne: function(day) {
                    var view = new DataRow({model: day});
                    this.$("#table-body-days").append(view.render().el);
                },
                refreshTotal: function() {
                    var work = 0;
                    Days.each(function($i) {
                        work += timeToFloat($i.get("workTime"));
                    });
                    this.total.html(floatToTime(work));
                }
            });

            function timeToFloat(time) {
                var parts = time.split(":");
                if (parts.length<2) return 0.0;
                return parseFloat(parts[0])+parseFloat(parts[1])/60;
            }

            function floatToTime(floatValue) {
                var fraction = floatValue - Math.floor(floatValue);
                var whole = Math.floor(floatValue);
                return whole+":"+Math.round(fraction*60.0);
            }


            var appview = new AppView;

        })($);
    };

    return {
        initialize: initialize
    };

});
//文件名:app.js
定义([
“jquery”,
“下划线”,
"骨干",,
“本地存储”
],函数($,\,主干){
var initialize=函数(){
(函数($){
var Day=Backbone.Model.extend({
初始化:函数(){
this.on(“更改”,this.calculateWorkTime);
这是.calculateWorkTime();
},
默认值:{
“日期”:新日期(),
“fromAm”:0,
“toAm”:0,
“fromPm”:0,
“toPm”:0,
“工作时间”:0
},
isValidTime:函数(n){
return!isNaN(parseFloat(n))&&isFinite(n)&&parseFloat(n)>0;
},
calculateWorkTime:函数(){
var功=0.0;
if(timeToFloat(this.get(“fromAm”))如果(parts.length)注释指出缺少保存调用。我将它们添加到了
DataRow.updateModel()
AppView.addDay()

这是正确的代码:

// Filename: app.js
define([
    'jquery',
    'underscore',
    'backbone',
    'localStorage'
], function($, _, Backbone){
    var initialize = function(){
        (function ($) {

            var Day = Backbone.Model.extend({
                initialize: function() {
                    this.on("change", this.calculateWorkTime);
                    this.calculateWorkTime();
                },
                defaults: {
                    "date":     new Date(),
                    "fromAm":   0,
                    "toAm":     0,
                    "fromPm":   0,
                    "toPm":     0,
                    "workTime": 0
                },
                isValidTime: function(n) {
                    return !isNaN(parseFloat(n)) && isFinite(n) && parseFloat(n)>0;
                },
                calculateWorkTime: function() {
                    var work = 0.0;
                    if (timeToFloat(this.get("fromAm")) < timeToFloat(this.get("toAm")) ) {
                        work += timeToFloat(this.get("toAm")) - timeToFloat(this.get("fromAm"));
                    }
                    if (timeToFloat(this.get("fromPm")) < timeToFloat(this.get("toPm")) ) {
                        work += timeToFloat(this.get("toPm")) - timeToFloat(this.get("fromPm"));
                    }
                    this.set("workTime", floatToTime(work));
                }
            });

            var DayList = Backbone.Collection.extend({
                model: Day,
                localStorage: new Backbone.LocalStorage("days")
            });

            var DataRow = Backbone.View.extend({
                tagName:  "tr",
                template: _.template($('#table-row-day').html()),
                events: {
                    "change .edit-field":  "updateModel",
                    "click #button-delete-day": "deleteDay"
                },
                initialize: function() {
                    this.listenTo(this.model, 'destroy', this.remove);
                },
                render: function() {
                    this.$el.html(this.template(this.model.toJSON()));
                    return this;
                },
                updateModel: function(event) {
                    var myValue = "leer";
                    switch (event.currentTarget.id) {
                        case "table-edit-date":
                            myValue = this.$('#table-edit-date').val();
                            this.model.set("date", myValue);
                            break;
                        case "table-edit-fromAm":
                            myValue = this.$('#table-edit-fromAm').val();
                            this.model.set("fromAm", myValue);
                            break;
                        case "table-edit-toAm":
                            myValue = this.$('#table-edit-toAm').val();
                            this.model.set("toAm", myValue);
                            break;
                        case "table-edit-fromPm":
                            myValue = this.$('#table-edit-fromPm').val();
                            this.model.set("fromPm", myValue);
                            break;
                        case "table-edit-toPm":
                            myValue = this.$('#table-edit-toPm').val();
                            this.model.set("toPm", myValue);
                            break;
                    }
                    this.model.save();
                    this.render();
                    console.log(this.model.toJSON());
                },
                deleteDay: function(a, b, c, d) {
                    this.model.destroy();
                }
            });

            var Days = new DayList;

            AppView = Backbone.View.extend({
                el: $("body"),
                initialize: function () {
                    this.listenTo(Days, 'add', this.addOne);
                    this.listenTo(Days, 'all', this.refreshTotal);
                    this.total = this.$("#total-work");
                    Days.fetch();
                    this.refreshTotal();
                },
                events: {
                    "click #button-add-day":  "addDay"
                },
                addDay: function () {
                    var day = new Day({
                        "date":     $("#form-add-date").val(),
                        "fromAm":   $("#form-add-fromAm").val(),
                        "toAm":     $("#form-add-toAm").val(),
                        "fromPm":   $("#form-add-fromPm").val(),
                        "toPm":     $("#form-add-toPm").val()
                    });
                    Days.add(day);
                    day.save();
                },
                addOne: function(day) {
                    var view = new DataRow({model: day});
                    this.$("#table-body-days").append(view.render().el);
                },
                refreshTotal: function() {
                    var work = 0;
                    Days.each(function($i) {
                        work += timeToFloat($i.get("workTime"));
                    });
                    this.total.html(floatToTime(work));
                }
            });

            function timeToFloat(time) {
                var parts = time.split(":");
                if (parts.length<2) return 0.0;
                return parseFloat(parts[0])+parseFloat(parts[1])/60;
            }

            function floatToTime(floatValue) {
                var fraction = floatValue - Math.floor(floatValue);
                var whole = Math.floor(floatValue);
                return whole+":"+Math.round(fraction*60.0);
            }


            var appview = new AppView;

        })($);
    };

    return {
        initialize: initialize
    };

});
//文件名:app.js
定义([
“jquery”,
“下划线”,
"骨干",,
“本地存储”
],函数($,\,主干){
var initialize=函数(){
(