Javascript Durandal敲除数组未定义

Javascript Durandal敲除数组未定义,javascript,jquery,knockout.js,durandal,Javascript,Jquery,Knockout.js,Durandal,我在从敲除数组中删除对象时遇到问题。我错了什么?它说:TypeError:that.active\u periods未定义。那么什么是错误的:那。活跃期。removeAll(自由日) periods.js define(function (require) { var backend = require('backend'); var ko = require('knockout'); var app = require('durandal/app

我在从敲除数组中删除对象时遇到问题。我错了什么?它说:TypeError:that.active\u periods未定义。那么什么是错误的:那。活跃期。removeAll(自由日)

periods.js

define(function (require) {
        var backend = require('backend');
        var ko = require('knockout');
        var app = require('durandal/app');

        return {
            active_periods:ko.observableArray([]),
            activate:function(){
                var that = this;
                backend.getActivePeriods().then(function(results){
                    that.active_periods(results);
                });
            },
            editActivePeriod:function(period){
                period.viewUrl = 'views/editActivePeriod';
                app.showDialog(period);
            },
            editFreeDays:function(){
                alert("hh");
            },
            deleteFreeDays:function(freeDay){
                var that = this;
                app.showMessage('Willst du wirklich diese freien Tag(e) löschen?', '', [ { text: "Ja", value: "Yes" }, { text: "Nein", value: "No" }],true, { style:  { width: "600px", height: "600" } }).then(function(response){
                if(response == "Yes"){
                    that.active_periods.removeAll(freeDay);
                }
                });
            }
        };
    });
periods.html

<section>
    <div class="row">
        <div class="col-md-6">
            <h3 class="center-block">aktive Perioden</h3>
            <hr>
            <button type="button" class="btn btn-success btn-lg btn-block"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> neue Periode erstellen</button>
            <br>
            <!-- ko foreach: active_periods -->
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <!--ko text: name--><!--/ko--> (<!--ko text: startDate--><!--/ko--> - <!--ko text: endDate--><!--/ko-->)<i class="fa fa-cog fa-lg pull-right" data-bind="click:$parent.editActivePeriod"></i>
                    </h3>
                </div>
                <div class="panel-body">
                    <ul data-bind="foreach: freeDays">
                        <li>
                            <!--ko text: name--><!--/ko-->
                            <i class="fa fa-times-circle fa-lg pull-right" data-bind="click:$parents[1].deleteFreeDays"></i>
                        </li>
                    </ul>
                </div>
            </div>
            <!-- /ko -->
        </div>
        <div class="col-md-6">
            <h3>archivierte Perioden</h3>
            <hr>
        </div>
    </div>
</section>

碱性周期蛋白

新周期
( - )
阿奇维耶特·佩尔登

在JavaScript中,正在构造的对象文本还不能引用自身#<代码>活动周期是在您所引用的文本上定义的

将代码更改为:

define(function (require) {
    var backend = require('backend');
    var ko = require('knockout');
    var app = require('durandal/app');

    var active_periods = ko.observableArray([]);  //<-Declared outside the reveal

    return {
        active_periods: active_periods, //<-Referenced here
        activate:function(){
            var that = this;
            backend.getActivePeriods().then(function(results){
                that.active_periods(results);
            });
        },
        editActivePeriod:function(period){
            period.viewUrl = 'views/editActivePeriod';
            app.showDialog(period);
        },
        editFreeDays:function(){
            alert("hh");
        },
        deleteFreeDays:function(freeDay){
            var that = this;
            app.showMessage('Willst du wirklich diese freien Tag(e) löschen?', '', [ { text: "Ja", value: "Yes" }, { text: "Nein", value: "No" }],true, { style:  { width: "600px", height: "600" } }).then(function(response){
                if(response == "Yes"){
                    that.active_periods.removeAll(freeDay);
                }
            });
        }
    };
});
我希望这有帮助

更新:增强的deleteFreeDays

在添加对下划线js库的引用后,我们进行了一些其他修改

define(function (require) {
    var    
        backend = require('backend'),
        ko = require('knockout'),
        _ = require('underscore'),    //<--Add a reference to UnderscoreJS (or LodashJS)
        app = require('durandal/app'),

        active_periods = ko.observableArray([]),

        activate = function() {
            backend.getActivePeriods().then(function(results){
                active_periods(results);
            });
        },

        editActivePeriod = function(period){
            period.viewUrl = 'views/editActivePeriod';
            app.showDialog(period);
        },

        editFreeDays = function(){
            alert("hh");
        },

        //Return an array where no element of the array equals freeDay
        _removeAllFreeDays = function (freeDay) {             //<--Private function: not exported
            return _.reject(active_periods, function (day) {
                return _.isEqual(day, freeDay);
            });
        }

        deleteFreeDays = function(freeDay){
            app.showMessage('Willst du wirklich diese freien Tag(e) löschen?', '', [ { text: "Ja", value: "Yes" }, { text: "Nein", value: "No" }],true, { style:  { width: "600px", height: "600" } }).then(function(response){
                if(response == "Yes"){
                    active_periods.removeAll();            //<--Empty observableArray
                    active_periods(_removeAllFreeDays(freeDay));  //<--Fill observableArray with new array devoid of freeDays                       
                }
            });
        };

    return {
        active_periods: active_periods,
        activate: activate,
        editActivePeriod: editActivePeriod,
        editFreeDays: editFreeDays,
        deleteFreeDays: deleteFreeDays
    };        
});
定义(功能(需要){
变量
backend=require('backend'),
ko=要求(“淘汰”),

_=require('下划线'),//在JavaScript中,正在构造的对象文字还不能引用自身。35;
活动期间
是在您引用的文字上定义的

将代码更改为:

define(function (require) {
    var backend = require('backend');
    var ko = require('knockout');
    var app = require('durandal/app');

    var active_periods = ko.observableArray([]);  //<-Declared outside the reveal

    return {
        active_periods: active_periods, //<-Referenced here
        activate:function(){
            var that = this;
            backend.getActivePeriods().then(function(results){
                that.active_periods(results);
            });
        },
        editActivePeriod:function(period){
            period.viewUrl = 'views/editActivePeriod';
            app.showDialog(period);
        },
        editFreeDays:function(){
            alert("hh");
        },
        deleteFreeDays:function(freeDay){
            var that = this;
            app.showMessage('Willst du wirklich diese freien Tag(e) löschen?', '', [ { text: "Ja", value: "Yes" }, { text: "Nein", value: "No" }],true, { style:  { width: "600px", height: "600" } }).then(function(response){
                if(response == "Yes"){
                    that.active_periods.removeAll(freeDay);
                }
            });
        }
    };
});
我希望这有帮助

更新:增强的deleteFreeDays

在添加对下划线js库的引用后,我们进行了一些其他修改

define(function (require) {
    var    
        backend = require('backend'),
        ko = require('knockout'),
        _ = require('underscore'),    //<--Add a reference to UnderscoreJS (or LodashJS)
        app = require('durandal/app'),

        active_periods = ko.observableArray([]),

        activate = function() {
            backend.getActivePeriods().then(function(results){
                active_periods(results);
            });
        },

        editActivePeriod = function(period){
            period.viewUrl = 'views/editActivePeriod';
            app.showDialog(period);
        },

        editFreeDays = function(){
            alert("hh");
        },

        //Return an array where no element of the array equals freeDay
        _removeAllFreeDays = function (freeDay) {             //<--Private function: not exported
            return _.reject(active_periods, function (day) {
                return _.isEqual(day, freeDay);
            });
        }

        deleteFreeDays = function(freeDay){
            app.showMessage('Willst du wirklich diese freien Tag(e) löschen?', '', [ { text: "Ja", value: "Yes" }, { text: "Nein", value: "No" }],true, { style:  { width: "600px", height: "600" } }).then(function(response){
                if(response == "Yes"){
                    active_periods.removeAll();            //<--Empty observableArray
                    active_periods(_removeAllFreeDays(freeDay));  //<--Fill observableArray with new array devoid of freeDays                       
                }
            });
        };

    return {
        active_periods: active_periods,
        activate: activate,
        editActivePeriod: editActivePeriod,
        editFreeDays: editFreeDays,
        deleteFreeDays: deleteFreeDays
    };        
});
定义(功能(需要){
变量
backend=require('backend'),
ko=要求(“淘汰”),

_=require('下划线'),//在JavaScript中,正在构造的对象文字还不能引用自身。35;
活动期间
是在您引用的文字上定义的

将代码更改为:

define(function (require) {
    var backend = require('backend');
    var ko = require('knockout');
    var app = require('durandal/app');

    var active_periods = ko.observableArray([]);  //<-Declared outside the reveal

    return {
        active_periods: active_periods, //<-Referenced here
        activate:function(){
            var that = this;
            backend.getActivePeriods().then(function(results){
                that.active_periods(results);
            });
        },
        editActivePeriod:function(period){
            period.viewUrl = 'views/editActivePeriod';
            app.showDialog(period);
        },
        editFreeDays:function(){
            alert("hh");
        },
        deleteFreeDays:function(freeDay){
            var that = this;
            app.showMessage('Willst du wirklich diese freien Tag(e) löschen?', '', [ { text: "Ja", value: "Yes" }, { text: "Nein", value: "No" }],true, { style:  { width: "600px", height: "600" } }).then(function(response){
                if(response == "Yes"){
                    that.active_periods.removeAll(freeDay);
                }
            });
        }
    };
});
我希望这有帮助

更新:增强的deleteFreeDays

在添加对下划线js库的引用后,我们进行了一些其他修改

define(function (require) {
    var    
        backend = require('backend'),
        ko = require('knockout'),
        _ = require('underscore'),    //<--Add a reference to UnderscoreJS (or LodashJS)
        app = require('durandal/app'),

        active_periods = ko.observableArray([]),

        activate = function() {
            backend.getActivePeriods().then(function(results){
                active_periods(results);
            });
        },

        editActivePeriod = function(period){
            period.viewUrl = 'views/editActivePeriod';
            app.showDialog(period);
        },

        editFreeDays = function(){
            alert("hh");
        },

        //Return an array where no element of the array equals freeDay
        _removeAllFreeDays = function (freeDay) {             //<--Private function: not exported
            return _.reject(active_periods, function (day) {
                return _.isEqual(day, freeDay);
            });
        }

        deleteFreeDays = function(freeDay){
            app.showMessage('Willst du wirklich diese freien Tag(e) löschen?', '', [ { text: "Ja", value: "Yes" }, { text: "Nein", value: "No" }],true, { style:  { width: "600px", height: "600" } }).then(function(response){
                if(response == "Yes"){
                    active_periods.removeAll();            //<--Empty observableArray
                    active_periods(_removeAllFreeDays(freeDay));  //<--Fill observableArray with new array devoid of freeDays                       
                }
            });
        };

    return {
        active_periods: active_periods,
        activate: activate,
        editActivePeriod: editActivePeriod,
        editFreeDays: editFreeDays,
        deleteFreeDays: deleteFreeDays
    };        
});
定义(功能(需要){
变量
backend=require('backend'),
ko=要求(“淘汰”),

_=require('下划线'),//在JavaScript中,正在构造的对象文字还不能引用自身。35;
活动期间
是在您引用的文字上定义的

将代码更改为:

define(function (require) {
    var backend = require('backend');
    var ko = require('knockout');
    var app = require('durandal/app');

    var active_periods = ko.observableArray([]);  //<-Declared outside the reveal

    return {
        active_periods: active_periods, //<-Referenced here
        activate:function(){
            var that = this;
            backend.getActivePeriods().then(function(results){
                that.active_periods(results);
            });
        },
        editActivePeriod:function(period){
            period.viewUrl = 'views/editActivePeriod';
            app.showDialog(period);
        },
        editFreeDays:function(){
            alert("hh");
        },
        deleteFreeDays:function(freeDay){
            var that = this;
            app.showMessage('Willst du wirklich diese freien Tag(e) löschen?', '', [ { text: "Ja", value: "Yes" }, { text: "Nein", value: "No" }],true, { style:  { width: "600px", height: "600" } }).then(function(response){
                if(response == "Yes"){
                    that.active_periods.removeAll(freeDay);
                }
            });
        }
    };
});
我希望这有帮助

更新:增强的deleteFreeDays

在添加对下划线js库的引用后,我们进行了一些其他修改

define(function (require) {
    var    
        backend = require('backend'),
        ko = require('knockout'),
        _ = require('underscore'),    //<--Add a reference to UnderscoreJS (or LodashJS)
        app = require('durandal/app'),

        active_periods = ko.observableArray([]),

        activate = function() {
            backend.getActivePeriods().then(function(results){
                active_periods(results);
            });
        },

        editActivePeriod = function(period){
            period.viewUrl = 'views/editActivePeriod';
            app.showDialog(period);
        },

        editFreeDays = function(){
            alert("hh");
        },

        //Return an array where no element of the array equals freeDay
        _removeAllFreeDays = function (freeDay) {             //<--Private function: not exported
            return _.reject(active_periods, function (day) {
                return _.isEqual(day, freeDay);
            });
        }

        deleteFreeDays = function(freeDay){
            app.showMessage('Willst du wirklich diese freien Tag(e) löschen?', '', [ { text: "Ja", value: "Yes" }, { text: "Nein", value: "No" }],true, { style:  { width: "600px", height: "600" } }).then(function(response){
                if(response == "Yes"){
                    active_periods.removeAll();            //<--Empty observableArray
                    active_periods(_removeAllFreeDays(freeDay));  //<--Fill observableArray with new array devoid of freeDays                       
                }
            });
        };

    return {
        active_periods: active_periods,
        activate: activate,
        editActivePeriod: editActivePeriod,
        editFreeDays: editFreeDays,
        deleteFreeDays: deleteFreeDays
    };        
});
定义(功能(需要){
变量
backend=require('backend'),
ko=要求(“淘汰”),


_=需要('下划线'),//虽然对象文字不能直接引用自身,但在对象文字上定义的函数可以引用对象文字本身(如果它被分配给变量)。因此,它们也可以将对象文字分配给变量并返回该变量,而不是返回文字方向。谢谢r你的努力,但我得到了一个两个解决方案,又是一个相同的错误。但现在错误在哪里?@fibi我道歉,但我不理解你的评论。@Dziamid得到了。谢谢。我试图保留他的原始意图。所以我在修改他的代码时没有走得太远。非常感谢你,不,它起作用了!但我仍然有问题,我想删除它这是一个对象,但它不起作用,我错了什么?虽然对象文字不能直接引用它自己,但在对象文字上定义的函数可以引用对象文字本身,如果它被赋给变量的话。因此,它们也可以简单地将对象文字赋给变量并返回它,而不是r转向字面上的方向。谢谢你的努力,但我得到了一个两种解决方案,同样的错误。但是现在错误在哪里?@fibi我道歉,但我不理解你的评论。@Dziamid得到了。谢谢。我试图保持他的原意。所以我在修改他的代码方面没有走得太远。非常感谢你,没有问题!Bu我还有一个问题,我想删除这个对象,但它不起作用,我错了什么?虽然一个对象文字不能直接引用它自己,但在一个对象文字上定义的函数可以引用对象文字本身,如果它被赋给一个变量。因此,它们也可以简单地赋给对象文字本身,而不是这里的任何一个解决方案o一个变量并返回它,而不是返回字面方向。谢谢你的努力,但我得到了一个两个解决方案,又是同一个错误。但是现在错误在哪里?@fibi,我很抱歉,但我不理解你的评论。@Dziamid得到了。谢谢。我试图保留他的原始意图。所以我在修改他的答案时没有走得太远代码。非常感谢,不,它可以工作!但我仍然有一个问题,我想删除这个对象,但它不工作,我错了什么?虽然对象文字不能直接引用它自己,但在对象文字上定义的函数可以引用对象文字本身,如果它被分配给变量。因此,它们不是这里的任何一种解决方案,而是uld还可以简单地将对象文字赋给一个变量并返回该变量,而不是返回文字方向。谢谢你的努力,但我得到了一个两种解决方案,又是同一个错误。但现在错误在哪里?@fibi我很抱歉,但我不理解你的评论。@Dziamid得到了。谢谢。我试图保留他的原意t、 所以我在修改他的代码时并没有走得太远。非常感谢你们,不,它是有效的!但我仍然有问题