简体   繁体   English

从javascript填充剔除视图模型

[英]Populate knockout view model from javascript

I'm in the process of replacing one hell of a lot of javascript/jquery code with knockoutjs and I'm trying to figure out the best way forward. 我正在用kickoutjs替换很多javascript / jquery代码的地狱,我正在尝试找出最好的方法。 I have no time to replace everything at the same time so I will have to integrate the knockout logic with the existing javascript... 我没有时间同时替换所有内容,因此我将不得不将剔除逻辑与现有的javascript集成在一起。

Is there a way to populate a knockout view model from javascript which is not called from a data-bind attribute? 有没有一种方法可以从javascript中填充剔除视图模型,而无需从数据绑定属性中调用该模型? Any help would be nice since I've not been able to find this anywhere else (at least not anything that worked). 因为我无法在其他任何地方找到它(至少没有任何可行的方法),所以任何帮助都是很好的。

I know what I'm mentioning here isn't the "correct" way of doing things, but I'm trying to migrate parts of the javascript code... Doing it all in one go isn't an option at the moment. 我知道我在这里提到的不是做事的“正确”方式,但是我正在尝试迁移javascript代码的一部分...目前暂时无法全部完成。

(using knockout 3.2) (使用淘汰赛3.2)

Edit: Typically the existing javascript does something like: 编辑:通常,现有的javascript会执行以下操作:

$('#productlist').append(productItemHtmlCode);

And I would rather have it do something like: 我宁愿它做类似的事情:

ViewModel.productList.push(productItemObject);

If I understand correctly, currently you have something like this: 如果我理解正确,当前您会遇到以下情况:

<div id='myDiv'>
       current status is: <span id='statusSpan'>Active</span>
</div>

with some corresponding javascript that might be something like: 与一些相应的JavaScript可能是这样的:

function toggleStatus() {
    var s= document.getElementById('statusSpan');
    s.innerHTML = s.innerHTML == 'Active' ? 'Inactive' : 'Active';
}

And you want to change it so that the javascript is updating the viewmodel rather than manipulating the DOM? 您想更改它,以便javascript更新视图模型而不是操作DOM吗?

var app = (function() {

    var vm = {
        statusText: ko.observable('Active'),
        toggleStatus: toggleStatus
    }

    return vm

    function toggleStatus() {
        vm.statusText = vm.statusText == 'Active' ? 'Inactive' : 'Active';
    }

}) ();

ko.applyBindings(app,document.getElementById('myDiv'));

And then the html would be 然后的HTML将是

<div id='myDiv'>
       current status is: <span id='statusSpan' data-bind="text: statusText"></span>
</div>

If that's what you're talking about, that's what Knockout is designed for. 如果您正在谈论的是那,那就是Knockout的目的。 The javascript updates the viewmodel, knockout manipulates the DOM. javascript更新视图模型,敲除操作DOM。

The example you give is easy to represent in Knockout. 您提供的示例很容易在Knockout中表示。

the HTML: HTML:

<div>
       <table data-bind="foreach: products">
            <tr>
                 <td data-bind="text: id"></td>
                 <td data-bind="text: name"></td>
                 <td data-bind="text: category"></td>
           </tr>
        </table>
    </div>

and in the viewmodel: 并在viewmodel中:

vm = {
    products: ko.observableArray(),  // empty array to start
    addProduct: addProduct
}

return vm;

function addProduct(id, name, category) {
    products.push({id: id, name: name, category:category});
}

etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM