在带有敲除和杜兰达尔的揭示原型模式中对“这个”的混淆
Confusion over "this" in the revealing prototype pattern with knockout and durandal
我正在尝试为我的应用程序制作基本视图模型,但我正在努力访问基本视图模型的上下文。
这是我的基本视图模型:
define(["config", 'services/logger'], function (config, logger) {
'use strict';
var
app = require('durandal/app'),
baseViewModel = function () {
this.items = ko.observableArray();
this.title = ko.observable();
this.selectedItem = ko.observable();
};
baseViewModel.prototype = (function () {
var
populateCollection = function (initialData, model) {
var self = this;
if (_.isEmpty(self.items())) {
ko.utils.arrayForEach(initialData, function (item) {
// self here works for all extending modules such as users
self.items.push(new model(item));
});
}
},
deleteItem = function (item) {
// "this" here same as prototype gives me window object :(
// so removing never happes
this.items.remove(item);
logger.log(config.messages.userMessages.confirmDeleted(item.Name()), {}, '', true);
},
confirmDelete = function (item) {
var
userMessage = config.messages.userMessages.confirmDelete(item.Type(), item.Name()),
negation = config.confirmationModalOptions.negation,
affirmation = config.confirmationModalOptions.affirmation;
app.showMessage(userMessage, 'Deleting ' + item.Type(), [affirmation, negation]).then(
function (dialogResult) {
dialogResult === affirmation ? deleteItem(item) : false;
});
};
return {
populateCollection: populateCollection,
confirmDelete: confirmDelete,
deleteItem: deleteItem
}
})();
return baseViewModel;
});
我使用这个非工作的东西的一个例子是:
define(['services/logger', 'models/user', 'viewmodels/baseviewmodel', 'services/dataservice'], function (logger, user, baseviewmodel, dataservice) {
var
users = new baseviewmodel();
users.title('Users')
users.searchTerm = ko.observable().extend({ persist: users.title() + '-searchterm' });
users.activate = function () {
this.populateCollection(dataservice.getUsers, user.model);
}
return users;
});
项目确实使用 populateCollection
正确填充。 confirmDelete
也会在模板中正确绑定,这可能是由于不需要上下文但是deleteItem
需要上下文,以便它可以访问items
并调用remove
。
如何正确访问
this
作为baseViewModel
的上下文 所以我可以用这种模式在我的方法中轻松引用它?
非常感谢
编辑:看起来this
在 deleteItem 中没有正确绑定,因为它是在 confirmDelete 内部的回调中调用的。因此,请在原始模型上尝试此代码段
confirmDelete = function (item) {
var self=this,
userMessage = config.messages.userMessages.confirmDelete(item.Type(), item.Name()),
negation = config.confirmationModalOptions.negation,
affirmation = config.confirmationModalOptions.affirmation;
app.showMessage(userMessage, 'Deleting ' + item.Type(), [affirmation, negation]).then(
function (dialogResult) {
dialogResult === affirmation ? self.deleteItem.call(self,item) : false;
});
};
奥尔,我的另一个解决方案。
我以前从未使用过"揭示原型模式",所以这里是如何在不坚持我们都不理解的模式的情况下做到这一点:)
我将所有内容声明为视图模型的属性并使用var self = this
,这样我的所有代码都非常明确。这是您的基本视图模型,已重写。如果所有视图模型逻辑都封装在视图模型中,并且视图模型在构造函数/函数定义中声明其依赖项,这通常很有帮助。
baseViewModel = function (dataService,userModel) {
var self = this;
self.items = ko.observableArray();
self.title = ko.observable();
self.searchTerm = ko.observable().extend({ persist: self.title() + '-searchterm' });
self.selectedItem = ko.observable();
self.populateCollection = function (initialData, model) {
if (_.isEmpty(self.items())) {
ko.utils.arrayForEach(initialData, function (item) {
// self here works for all extending modules such as users
self.items.push(new model(item));
});
}
};
self.activate = function () {
this.populateCollection(dataservice.getUsers, userModel);
}
self.deleteItem = function (item) {
// "this" here same as prototype gives me window object :(
// so removing never happes
self.items.remove(item);
logger.log(config.messages.userMessages.confirmDeleted(item.Name()), {}, '', true);
};
self.confirmDelete = function (item) {
var
userMessage = config.messages.userMessages.confirmDelete(item.Type(), item.Name()),
negation = config.confirmationModalOptions.negation,
affirmation = config.confirmationModalOptions.affirmation;
app.showMessage(userMessage, 'Deleting ' + item.Type(), [affirmation, negation]).then(
function (dialogResult) {
dialogResult === affirmation ? self.deleteItem(item) : false;
});
};
};
请注意,因为我还将依赖项移动到视图模型构造函数中,所以现在您的初始化代码如下所示:
define(['services/logger', 'models/user', 'viewmodels/baseviewmodel', 'services/dataservice'], function (logger, user, baseviewmodel, dataservice) {
var users = new baseviewmodel(dataservice,user.model);
users.title('Users')
return users;
});
相关文章:
- 模块模式和这个
- 为什么除了html5输入模式属性之外,这个简单的regex在任何地方都能工作
- 在twitter引导模式上分享这个
- 什么'这是这个JavaScript模式的名称
- 为什么这个jQuery颜色选择器在引导模式中失败
- 在带有敲除和杜兰达尔的揭示原型模式中对“这个”的混淆
- 为什么这个 ng-idle 实现不连接模式和样式
- 这个特定 csv 数据集的 MongoDB 模式?(用于数据目的)
- 为什么这个推特引导模式在使用淘汰 js 时不起作用
- Javascript 揭示模块模式 - 这个和内部函数调用
- 对这个javascript模式感到困惑
- 在这个特定的例子中,为什么作者在for中使用if.hasOwnProperty..循环模式
- 为什么这个代码在“”中抛出undefined;严格模式”;
- $http和factory-这个模式是如何工作的
- JavaScriptREGEX:给我所有的东西,除了这个模式之间的东西
- 我想了解这个javascript函数模式
- Javascript模块模式、范围和“;这个“;
- 浏览器中的观察者模式javascript:订阅事件"添加了Dom元素“;并处理这个元素
- 有没有更好的方法/模式来编写这个jQuery插件
- Javascript-如何保存对“;这个“;用于使用原型模式在命名函数回调中进行访问