不带任何参数调用ko.applyBindings是否安全? [英] Is it safe to call ko.applyBindings without any parameters?

查看:78
本文介绍了不带任何参数调用ko.applyBindings是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我可以在ko.applyBindings()上找到的唯一官方文档:

This is the only official doc I could find on ko.applyBindings():

http://knockoutjs.com/documentation/observables.html

这不是真正的正式书面文件,没有确切说明什么是可选的/etc.在测试中,似乎调用ko.applyBindings()允许在全局范围内进行绑定,并且看起来工作正常.有没有人研究过源代码(或者比我更了解KO),知道这是否安全?有性能问题吗?

It's not a real formal written document that says exactly what is optional/etc. In testing it appears that calling ko.applyBindings() allows for bindings on a global scope, and it appears to work fine. Has anyone that studied the source code (or know more about KO than I do), know if this is safe or not? Any performance issues?

这是我正在使用的SPA的"app"对象的开始:

This is the start of my "app" object for an SPA I'm working on:

var app = {

self: this,
datacontext: new DataContext(),
dataservice: new DataService(),
viewModels: {
    main: new MainViewModel(),
    folderDetails: new FolderDetailsViewModel()
},

init: function() {

    ko.applyBindings();

    Sammy(function() {

        this.get('#:folder', function() {
            self.doFolderRoute(this.params.folder);
        });

        // Override this function so that Sammy doesn't mess with forms
        this._checkFormSubmission = function(form) {
            return (false);
        };

    }).run();

},

doFolderRoute: function(id) {
    console.log("doFolderRoute: " + id);
}
}

我可以做ko.applyBindings(self.viewModels)来将数据绑定到模型中...但是我有点喜欢能够绑定到任何东西并且不介意(甚至喜欢)打字的自由在我的代码中删除了app.viewModels.main,而不是"main".

I could do ko.applyBindings(self.viewModels), to restrict data-bind to the models... but I kind of like the freedom of being able to bind to anything and don't mind (even like) typing out app.viewModels.main in my code, vs just "main".

推荐答案

通过使用ko.applyBindings(),淘汰赛不会知道您的视图模型.在根级别将不会设置$root,也不会设置$data.显然,您根本无法引用这些变量.事件绑定还将$data传递给事件处理函数,在本例中为undefined.

By using ko.applyBindings(), Knockout won't know about your view model. $root won't be set and neither will $data at the root level. Obviously, you can simply not reference those variables. Also the event bindings pass $data to the event handler function, which in this case will be undefined.

这篇关于不带任何参数调用ko.applyBindings是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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