首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Durandal收听事件

在Durandal收听事件
EN

Stack Overflow用户
提问于 2014-02-06 06:45:37
回答 2查看 2.5K关注 0票数 0

我正在查看Durandal文档,但我找不到侦听Durandal events的具体实现,例如router events

有没有人能给我指一指文档,或者(如果没有文档的话)一个例子?

EN

回答 2

Stack Overflow用户

发布于 2014-02-06 22:16:49

在视图模型中,您应该监听激活器事件。Link。从Durandal starter模板中查看此示例。它正在侦听activate和canDeactivate事件:

代码语言:javascript
复制
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']);
        }
    };
});
票数 1
EN

Stack Overflow用户

发布于 2014-02-12 23:22:45

下面是我工作过的项目中的一些示例代码:

代码语言:javascript
复制
//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);
      }

});        
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21590227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档