jquery - KnockoutJS get JSON from remote source with mapping plugin binding issue -
i have jquery:
$.ajax({url : surl, datatype: "jsonp", type: "get", contenttype: "application/json;charset=utf-8", headers: { "accept": "application/json;charset=utf-8" }, error: function (a, b, c){ alert(a); alert(a.status); alert(b); alert(c); }, success: function (data) { alert(data); var viewmodel = ko.mapping.fromjs(data); alert(viewmodel); /*$.each(data, function (index, element) { alert(index); alert(element); });*/ } });
the alert(data);
gives me this:
second alert gives me:
function c(){if(0<arguments.length)return c.equalitycomparer&&c.equalitycomparer(d,arguments[0])||(c.k(),d=arguments[0],c.j()),this;a.q.bb(c);return d}
the .each
run until index > 100, there row set returned.
here small piece of json (when manually hit url var surl
:
([{ "0":"c551003", "id":"57024", "1":"0373", "number":"373-nik", "2":"1349078013", "date_time":"1349078192", // etc. },{ // next item
i have no clue best approach parse json nice table. thought give me array of rows in viewmodel , this: http://knockoutjs.com/documentation/foreach-binding.html have no idea if mapping worked , how can bind instance number
prop.
<tbody data-bind="foreach: ihavenoclue"> <tr> <td data-bind="text: 0"></td> <td data-bind="text: id"></td> <td data-bind="text: 1"></td> <td data-bind="text: number"></td> <td data-bind="text: 2"></td> <td data-bind="text: date_time"></td> </tr> </tbody>
note front: because have properties name number, you'll have jump through hoops text
bindinghandler work. 1 way replace text: 1
text: $data['1']
.
you on right track. ihavenoclue
can replaced $root
, .e.g:
<tbody data-bind="foreach: $root">
see this fiddle example. works, because viewmodel made observablearray
ko.mapping plugin.
alternative solution
alternatively, can create view model holds items observable array stuff ajax request. view model constructed this:
var viewmodel = { title: ko.observable("my title"), items: ko.mapping.fromjs(data) };
see this fiddle demo of version. in example viewmodel simple object, create constructor function builds more complex view model.
Comments
Post a Comment