0
我在審閱Durandal文檔,但找不到Durandal events的具體實施方法,例如router events。收聽Durandal的活動
有人可以指向我的文檔,或者(如果沒有關於此的文檔)的例子?
我在審閱Durandal文檔,但找不到Durandal events的具體實施方法,例如router events。收聽Durandal的活動
有人可以指向我的文檔,或者(如果沒有關於此的文檔)的例子?
在您的視圖模型中,您應該聽激活事件。 Link。從Durandal初學者模板中檢查這個例子。它是聽激活和canDeactivate事件:
define(['plugins/http', 'durandal/app', 'knockout'], function (http, app, ko) {
//Note: This module exports an object.
//That means that every module that "requires" it will get the same object instance.
//If you wish to be able to create multiple instances, instead export a function.
//See the "welcome" module for an example of function export.
return {
displayName: 'Flickr',
images: ko.observableArray([]),
activate: function() {
//the router's activator calls this function and waits for it to complete before proceding
if (this.images().length > 0) {
return;
}
var that = this;
return http.jsonp('http://api.flickr.com/services/feeds/photos_public.gne', { tags: 'mount ranier', tagmode: 'any', format: 'json' }, 'jsoncallback').then(function(response) {
that.images(response.items);
});
},
select: function(item) {
//the app model allows easy display of modal dialogs by passing a view model
//views are usually located by convention, but you an specify it as well with viewUrl
item.viewUrl = 'views/detail';
app.showDialog(item);
},
canDeactivate: function() {
//the router's activator calls this function to see if it can leave the screen
return app.showMessage('Are you sure you want to leave this page?', 'Navigate', ['Yes', 'No']);
}
};
});
下面是該項目一些示例代碼,我在工作:
//authentication.js
define(['durandal/events'], function(events){
var authentication = {};
events.includeIn(authentication);
//perform login then trigger events to whoever is listening...
authentication.trigger('logged:on',user);
//perfom logoff then trigger events to whoever is listening...
authentication.trigger('logged:off');
return {
authentication: authentication
}
});
//logon.js
//pull in authenticaion
define(['authentication'], function(authentication){
authentication.on('logged:on',loggedOn);
//callback that gets called when the logged:on event is fired on authentication
function loggedOn(user){
console.log(user);
}
});
感謝您的信息!我知道你可以放置特定的回調函數,如果它們存在的話,它們將在Durandal生命週期的特定點被調用,但這不是我認爲的「添加事件監聽器」。因爲杜蘭達有這些東西叫做「事件」,我假設有一個杜蘭達相當於「addEventListener」......? – Josh
查看[利用發佈和訂閱](http://www.durandaljs.com/documentation/Leveraging-Publish-Subscribe.html) –