Durandal自定义视图定位策略

Durandal自定义视图定位策略,durandal,Durandal,我试图弄清楚如何使用自定义视图位置策略,我已经阅读了本页的文档,但我不完全理解策略函数应该是什么样子 有谁能给我一个快速的例子,说明这个函数的实现是什么样的,以及返回的承诺(即使是一个简单的)等等 提前感谢,, 加里 p、 这是我的html中的代码: <div> <div data-bind="compose: {model: 'viewmodels/childRouter/first/simpleModel', strategy: 'viewmodels/chi

我试图弄清楚如何使用自定义视图位置策略,我已经阅读了本页的文档,但我不完全理解策略函数应该是什么样子

有谁能给我一个快速的例子,说明这个函数的实现是什么样的,以及返回的承诺(即使是一个简单的)等等

提前感谢,, 加里

p、 这是我的html中的代码:

 <div>
     <div data-bind="compose: {model: 'viewmodels/childRouter/first/simpleModel', strategy:
 'viewmodels/childRouter/first/myCustomViewStrategy'}"></div> </div>
但我得到了一个错误:


未捕获类型错误:无法读取未定义的属性“display”-这是在控制台窗口中记录完成后发生的。

好的,我通过以下方法创建自定义视图策略解决了此问题:

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

    var myCustomViewStrategy = function () {
        return viewEngine.createView('views/childRouter/first/sModelView');
    }

    return myCustomViewStrategy;

});

当我发现文档中缺少关于compose binding的策略设置时,我检查了源代码的工作原理。总而言之:

组合绑定的策略设置通过其moduleId指定的模块

  • 必须返回名为“strategy”的函数
  • 它返回一个承诺,该承诺导致视图受到约束
  • 作为HTML元素对象
  • strategy方法作为参数接收compose绑定的设置对象
  • 模型对象已解析
一个有效的例子:

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;
});  
杜兰达尔的资料来源:

// 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);
    });
}

p、 我宁愿通过理解来学习,所以如果有人能给我指出正确的方向,我将不胜感激。
// 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);
    });
}