我们使用 jquery 和 requirejs 来创建一个像这样的“viewmodel”:
define('vm.inkoopfactuurAanleveren',
['jquery', 'underscore', 'ko', 'datacontext', 'router', 'messenger', 'config', 'store'],
function ($, _, ko, datacontext, router, messenger, config, store) {
var
isBusy = false,
isRefreshing = false,
inkoopFactuur = { factuurNummer: ko.observable("AAA") },
activate = function (routeData, callback) {
messenger.publish.viewModelActivated({ canleaveCallback: canLeave });
getNewInkoopFactuurAanleveren(callback);
var restricteduploader = new qq.FineUploader({
element: $('#restricted-fine-uploader')[0],
request: {
endpoint: 'api/InkoopFactuurAanleveren',
forceMultipart: true
},
multiple: false,
failedUploadTextDisplay: {
mode: 'custom',
maxChars: 250,
responseProperty: 'error',
enableTooltip: true
},
text: {
uploadButton: 'Click or Drop'
},
showMessage: function (message) {
$('#restricted-fine-uploader').append('<div class="alert alert-error">' + message + '</div>');
},
debug: true,
callbacks: {
onComplete: function (id, fileName, responseJSON) {
var response = responseJSON;
},
}
});
},
invokeFunctionIfExists = function (callback) {
if (_.isFunction(callback)) {
callback();
}
},
loaded = function (factuur) {
inkoopFactuur = factuur;
var ids = config.viewIds;
ko.applyBindings(this, getView(ids.inkoopfactuurAanleveren)); /*<----- THIS = OUT OF SCOPE!*/ /
},
bind = function () { },
saved = function (success) {
var s = success;
},
saveCmd = ko.asyncCommand({
execute: function (complete) {
$.when(datacontext.saveNewInkoopFactuurAanleveren(inkoopFactuur))
.then(saved).always(complete);
return;
},
canExecute: function (isExecuting) {
return true;
}
}),
getView = function (viewName) {
return $(viewName).get(0);
},
getNewInkoopFactuurAanleveren = function (callback) {
if (!isRefreshing) {
isRefreshing = true;
$.when(datacontext.getNewInkoopFactuurAanleveren(dataOptions(true))).then(loaded).always(invokeFunctionIfExists(callback));
isRefreshing = false;
}
},
dataOptions = function (force) {
return {
results: inkoopFactuur,
// filter: sessionFilter,
//sortFunction: sort.sessionSort,
forceRefresh: force
};
},
canLeave = function () {
return true;
},
forceRefreshCmd = ko.asyncCommand({
execute: function (complete) {
//$.when(datacontext.sessions.getSessionsAndAttendance(dataOptions(true)))
// .always(complete);
complete;
}
}),
init = function () {
// activate();
// Bind jQuery delegated events
//eventDelegates.sessionsListItem(gotoDetails);
//eventDelegates.sessionsFavorite(saveFavorite);
// Subscribe to specific changes of observables
//addFilterSubscriptions();
};
init();
return {
activate: activate,
canLeave: canLeave,
inkoopFactuur: inkoopFactuur,
saveCmd: saveCmd,
forceRefreshCmd: forceRefreshCmd,
bind: bind,
invokeFunctionIfExists: invokeFunctionIfExists
};
});
在“加载”方法的行ko.applyBindings(this, getView(ids.inkoopfactuurAanleveren));
中,“this”关键字不引用“viewmodel”对象。“self”关键字似乎是指在多个“viewmodels”上找到的方法的组合。saveCmd 属性是通过敲除绑定的,但由于找不到它而给出错误。
ko.applyBindings 如何获得对视图模型的正确引用?换句话说,我们需要用什么来替换applyBindings 中的'this' 关键字。
我想你可以“要求”requirejs 给我们带有标识符“vm.inkoopfactuurAanleveren”的 ealiers 实例化对象,但我不知道如何。