English 中文(简体)
Knockout JS编制一份项目清单
原标题:Knockout JS building a list of items

我正试图做的是,从其他一些较大的现有物品清单中拟定一份清单(就背景而言,具体列出某种产品的成分)。 在Knockout问题上,我仍然很抱着绿色态度,并想知道我是否失踪,因为我目前正在使用的解决办法是错的。 我有两套基本标准模板,并使用对我的看法模式的两个可观察阵列(现有成分清单中一个,产品特定成分清单中一个)具有约束力的信标。

<div data-bind="template: {name: ingredientTypeTemplate , foreach: ingredientTypes}"></div>

<div data-bind="template: {name: selectedIngredientTypeTemplate , foreach: selectedIngredientTypes}"></div>

<script>

var productTypeViewModel = {

    ingredientTypes: ko.observableArray([]),
    selectedIngredientTypes: ko.observableArray([]),

    addIngredient: function () {
        productTypeViewModel.ingredientTypes.remove(this);
        productTypeViewModel.selectedIngredientTypes.push(this);
    },

    removeIngredient: function () {
        productTypeViewModel.selectedIngredientTypes.remove(this);
        productTypeViewModel.ingredientTypes.push(this);
    },
};

$(document).ready(function () {

    $.getJSON("/IngredientType/Index")
            .success(function (data) {
                productTypeViewModel.ingredientTypes($.parseJSON(data));
            })
            .error(function () { alert("error"); });

    ko.applyBindings(productTypeViewModel);
});

</script>

<script type="text/x-jquery-tmpl" id="ingredientTypeTemplate">
    <div>
        <span>${Name}</span>
        <a href= #  data-bind="click: productTypeViewModel.addIngredient">Add</a>
    </div>
</script>

这是我试图与克尼博特做些什么的最佳方式吗? 有些人感到错失,尽管我不敢肯定什么。 我确实对成分有单独的后遗体定义。 具有附加功能的类型。 能够观测到的成分微粒将随着这种类型的一系列的后继器而初步形成,该模板当时刚刚使用数据组合式的“click:添加”,但我确实不得不从处理点击的那部分中向主要的后方通报。

基本上,我只想说明我所做的工作是标准做法,还是更好(更适合Knockout)。

最佳回答
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签