Durandal Custom View定位策略 [英] Durandal Custom View Location Strategy

查看:46
本文介绍了Durandal Custom View定位策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何使用自定义视图位置策略,我已经阅读了此页面上的文档 http://durandaljs.com/documentation/Using-Composition/ ,但我不完全了解该策略功能的外观。

I am trying to figure out how to use a custom view location strategy, I have read the documentation at this page http://durandaljs.com/documentation/Using-Composition/ but I don't exactly understand what the strategy function should look like.

谁能给我一个简单的例子,说明此函数的实现是什么样的,以及返回的承诺(甚至是简单的实现)等?

Can anybody give me a quick example of what the implementation of this function would be like and the promise that returns (even a simple one) etc?

在此先感谢,
Gary

Thanks in advance, Gary

ps这是我html中的代码:

p.s. This is the code in my html:

 <div>
     <div data-bind="compose: {model: 'viewmodels/childRouter/first/simpleModel', strategy:
 'viewmodels/childRouter/first/myCustomViewStrategy'}"></div> </div>

这是我的myCustomViewStrategy中的代码:

and this is the code in my myCustomViewStrategy:

define(function () {

    var myCustomViewStrategy = function () {

        var deferred = $.Deferred();

        deferred.done(function () { console.log('done'); return 'simpleModelView'; });
        deferred.fail(function () { console.log('error'); });

        setTimeout(function () { deferred.resolve('done'); }, 5000);

        return deferred.promise();
    };

return myCustomViewStrategy;
});

但我得到了错误:

Uncaught TypeError:无法读取未定义的属性 display-这是在控制台窗口中记录完之后。

Uncaught TypeError: Cannot read property 'display' of undefined - this is after done has been logged in the console window.

推荐答案

As我发现文档缺少有关组合绑定的策略设置的信息,我检查了源代码的工作方式。总结一下:

As I found the documentation a bit lacking on compose binding's strategy setting I checked the source code how it works. To summ it up:

由组合绑定的策略设置指定的模块,其模块ID

The module specified by the compose binding's strategy setting by its moduleId


  • 必须返回一个名为 strategy的函数

  • ,该函数返回一个诺言,该诺言将导致视图被绑定

  • 作为HTML元素目的。

  • 作为方法的策略方法,参数将接收已绑定模型对象的组合绑定的设置对象


  • must return a function named 'strategy'
  • which returns a promise which results in the view to be bound
  • as a HTML element object.
  • As a parameter the strategy method receives the compose binding's settings object
  • with the model object already resolved.

一个工作示例:

define(['durandal/system', 'durandal/viewEngine'], function (system, viewEngine) {

    var strategy = function(settings){
        var viewid = null;
        if(settings.model){
            // replaces model's module id's last segment ('/viewmodel') with '/view'
            viewid = settings.model.__moduleId__.replace(/\/[^\/]*$/, '/view');
        }
        return viewEngine.createView(viewid);
    };

    return strategy;
});  

Durandal的来源:

Durandal's source:

// composition.js:485

for (var attrName in settings) {
    if (ko.utils.arrayIndexOf(bindableSettings, attrName) != -1) {
        /*
         * strategy is unwrapped
         */
        settings[attrName] = ko.utils.unwrapObservable(settings[attrName]);
    } else {
        settings[attrName] = settings[attrName];
    }
}

// composition.js:523

if (system.isString(context.strategy)) {
    /*
     * strategy is loaded
     */
    system.acquire(context.strategy).then(function (strategy) {
        context.strategy = strategy;
        composition.executeStrategy(context);
    }).fail(function(err){
        system.error('Failed to load view strategy (' + context.strategy + '). Details: ' + err.message);
    });
} else {
    this.executeStrategy(context);
}

// composition.js:501

executeStrategy: function (context) {
    /*
     * strategy is executed
     * expected to be a promise
     * which returns the view to be bound and inserted to the DOM
     */
    context.strategy(context).then(function (child) {
        composition.bindAndShow(child, context);
    });
}

这篇关于Durandal Custom View定位策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆