English 中文(简体)
带有Knockout.js和ko的单一尺寸阵列的模板。 制图
原标题:Template for a single dimension array with Knockout.js and ko.mapping

我有一位智者说,我正在用K.mapping plugin绘制地图,我需要为地图阵列之一建立可观察到的约束力。 阵列为[1,2,3,4,5],因此没有指数。

I have the following code working to a certain point:

<script id="statRowTemplate" type="text/html">
    {{if type() != "column"}}
    <tr>
            <td><label>Name: </label><input data-bind="value: name" /></td>
            <td><label>Plot Points: </label><ul data-bind="template: {name:  dataTemplate  , foreach: data}"></ul> </td>
    </tr>
    {{/if}}
</script>
<script type="text/html" id="dataTemplate">         
<li>
    <p>${this.data}</p>
        <input data-bind="value: this.data" />
</li>
</script>
<button data-bind="click: saveChanges">Save Changes</button>

So everything is working as expected except <input data-bind="value: this.data" /> That field remains blank ... ${this.data} Shows the correct value however.

Overall, I am tying to update a JSON string and re-save it in a flat file. I can bind the data array: [1,2,3,4] directly to the input value but then it does not update as an array.

Here is the viewModel: var viewModel = {};

$.getJSON( /data-forecast-accuracy.json , function(result) {

    viewModel.stats = ko.mapping.fromJS(result);
    console.log(result)
    viewModel.saveChanges = function() {

        var unmapped = ko.mapping.toJSON(viewModel.stats);
        console.log(unmapped);

        $.post( save-changes.php , {data: unmapped})
        .success(function() { console.log("second success"); })
        .error(function() {  console.log("error"); })
        .complete(function() {  console.log("complete"); });
    }

    ko.applyBindings(viewModel);
});

Here is the JSON:

[ { "type":"spline", "marker":{ "symbol":"diamond" }, "name":"Cumulative", "data":[10,17,18,18,16,17,18,19] }, { "type":"spline", "marker":{ "symbol":"circle" }, "name":"Monthly", "data":[10,22,20,19,8,20,25,23] } ]

Any help would be greatly appreciated. Also open to suggestions if I am approaching this from the wrong angle. Ultimately just want to be able to modify the JSON sting through a UI and then save it.

Thanks in advance.

最佳回答

为了适当贴出阵列的价值,你将想把原阵列图成含有受<代码>约束的实际财产的结构[1,2,3]<代码>改为<代码>[{: 1},{val: 2},{val: 3}/code>。 对照<代码>数据的标签将不起作用,因为KO无法确定如何从用户投入中更新数据(目前无法理解其位于一个阵列的某些指数)。

我愿做一些事情:http://jsfiddle.net/rniemeyer/v2Wx/

http://www.knockmeout.net/204/controlling-how-object-is-converted-to.html” rel=“nofollow”>this,使阵列在变成JSON时自动看上像原样。

问题回答

暂无回答




相关问题
How to go from DOM node to viewModel object?

When the drop function is called back, this is set to the droppable DOM node (the target) and ui.draggable is the DOM node which was dragged. Is there an idiomatic way of getting the model object ...

making fields observable after ajax retrieval in knockout.js

I am wondering how I can make certain fields observables in knockout.js that I get from an ajax call without having to define the whole object in my viewmodel. Is this possible? Here is what I have ...

ASP.NET MVC Validation with jQuery $.ajax

I have a situation where I am sending data to a Controller via jQuery $.ajax, the data is a serialized json string. (MVC 3.0) It binds it fine - my controller receives the results and they are ...

Knockout.js and MVC

Just started playing with knockout.Js which is a fantastic framework Steve s really done well with that one. One thing I can t seem to do at the minute is impliment it with my Html Helpers. So for ...

Anyone using Knockoutjs with asp.net-mvc in anger? [closed]

I find it very interesting and have a prototype working based on Steve s mvc sample and another small sample from this thread. Using json.net to deserialize within the post action as I couldn t ...

热门标签