2

我正在为 asp.net mvc SPA 探索 durandaljs。我正在使用在 hottowel SPA 示例下找到的 APS.net MVC4、Durandaljs、Knockoutjs、breeze、moment 和其他库。

我有一个与 DOB、DateTime 绑定的客户端视图。

    <td colspan="2">
                    <span id="dob" data-bind="text: DOB"></span>
            </td>                                                

我的 ViewModel 包含代码

    vm.studentProfile().DOB(moment(vm.studentProfile().DOB()).format('L'));
        logger.log(vm.studentProfile().DOB(), null, system.getModuleId(vm), false);

上面的代码实际上来自querySucceeded。IE

    return manager
        .executeQuery(query)
        .then(querySucceeded)
        .fail(queryFailed);

这应该是有效的,因为我已经为其他一些字段实现了这一点,但是如果 DateTime KnockoutOut 不会更新 GUI,而我可以在控制台日志中看到 UPDATED 格式日期。有人可以告诉我我在这里缺少什么。提前致谢。

4

2 回答 2

1

问题可能在于 DOB 是MomentJs日期,而不是 JavaScriptDate或字符串。您很可能需要添加自定义绑定处理程序来显示这些日期,例如:

ko.bindingHandlers.moment = {
            update: function(element, valueAccessor) {
                var value = valueAccessor();
                var formattedValue = ko.utils.unwrapObservable(value).format('LLLL');
                $(element).text(formattedValue);
            }
};

现在,不要使用“text”绑定处理程序,而是使用“moment”绑定处理程序,如下所示:

<span id="dob" data-bind="moment: DOB"></span>

编辑:添加了一个使用 AMD 模块和 RequireJS 添加自定义插件的示例:

require(['jquery', 'json2', 'sammy', 'amplify', 'bootstrap', 'moment', 'toastr', 'showdown', 'markdowneditor', 'spin'], 
        function($){

        // Require that plugins be loaded, after the prerequisite libraries
        //       We load the plugins here and now so that we don't have to 
        //       name them specifically in the modules that use them because
        //       we don't want those modules to know that they use plugins.
        requirejs([
                'jquery.ui',                // jquery plugin
                'jquery.mockjson',          // jquery plugin
                'jquery.tmpl',          // jquery plugin
            ], 
            function () { 
                require(['ko'],
                    function(ko) {
                        // ensure KO is in the global namespace ('this') 
                        if (!this.ko) {
                            this.ko = ko;
                        };

                        requirejs([
                                'libs/knockout.binding.handlers',       // Knockout custom binding handlers
                                'libs/knockout.extenders',       // Knockout custom binding handlers
                                'libs/bootstrap.extenders',       // Knockout custom binding handlers
                            ],
                            // Plugins generally don't return module objects
                            // so there would be point in passing parameters to the function
                            function () { 
                                require(['app'], function(App) {
                                    App.initialize(); 
                                });
                            }
                        );
                    }
                );
            }
        );
    }
);
于 2013-05-14T16:39:51.030 回答
0

只做一个 ko 怎么样。计算如下

vm.studentProfileFormatted = ko.computed({
  read: function () {
    return moment(vm.studentProfile().DOB()).calendar();
  },
  write: function (value) {
    var time = moment(value, "MM-DD-YYYY").toJSON();
      vm.studentProfile(time);
  },
  owner: vm
});

然后在您的视图中调用 studentProfileFormatted。

于 2013-05-16T15:51:40.540 回答