【问题标题】:KO.js howto set default view when using view constructorKO.js如何在使用视图构造函数时设置默认视图
【发布时间】:2014-05-30 10:55:02
【问题描述】:

我(KO 新手!)在此处使用 Ryan Niemayer 提供的视图构造函数:How to properly structure a KnockoutJS application,我正在尝试设置默认视图以在加载时显示...

例如在#3中,你可以使用一个构造函数,比如……

这是我尝试设置默认值的叉子:

// (templates are defined in the fiddle)
var View = function(title, templateName, data) {
   this.title = title;
   this.templateName = templateName;
   this.data = data; 
};

var definedViews = ko.observableArray([
    new View("one", "oneTmpl", new SubModelA()),
    new View("two", "twoTmpl", new SubModelB())
]);

var viewModel = {
    views: definedViews,
    defaultView: 0,
    selectedView: ko.observable( definedViews[0] )
};

ko.applyBindings(viewModel);

index.html:

    <!-- ko with: selectedView -->
    <div data-bind="template: { name: templateName, data: data }"></div>
    <!-- /ko -->

http://jsfiddle.net/memeLab/WVVyM/2/

加载时,selectedView 未定义,但是当我单击选择时,它包含预期的对象...

我还尝试将 viewModel 重构为一个函数,尝试将参数提供给 html 声明,以及一堆随机的猜测,不够连贯,无法在这里总结……

有什么建议吗?蒂亚!

【问题讨论】:

    标签: knockout.js


    【解决方案1】:

    其实你少了()

    var viewModel = {
        views: definedViews,
        defaultView: 0,
        selectedView: ko.observable( definedViews()[0] )
    };
    

    这里是工作小提琴:http://jsfiddle.net/ZAHC9/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      相关资源
      最近更新 更多