我是knockout.js
的新手,我有一个问题。我找到了一些代码here。我的get函数返回{"id":"1", "name":"cardio", "image":"aslas", "header":"Kardio", "message":"message\r\n"}
但它不起作用。谁能帮我?谢谢
<ul class="folders" data-bind="foreach: folders">
<li data-bind="text: $data,
css: { selected: $data == $root.chosenFolderId() },
click: $root.goToFolder"></li>
</ul>
<div class="mails" data-bind="with: chosenFolderData">
<p><label>From</label>: <span data-bind="text: name"></span></p>
</div>
<script>
function WebmailViewModel() {
// Data
var self = this;
self.folders = ['cardio', 'classic', 'mass', 'abs']
;
self.chosenFolderId = ko.observable();
self.chosenFolderData = ko.observable();
self.chosenMailData = ko.observable();
// Behaviours
self.goToFolder = function(folder) {
self.chosenFolderId(folder);
self.chosenMailData(null);
$.get(ajax_url+'Dieticians/video/'+folder,null, self.chosenFolderData);
};
self.goToFolder('Inbox');
};
ko.applyBindings(new WebmailViewModel());
</script>
答案 0 :(得分:0)
这是你在找什么?:http://jsfiddle.net/58tkrbd9/3/
<ul class="folders" data-bind="foreach: folders">
<li data-bind="text: $data,
css: { selected: $data == $root.chosenFolderId() },
click: $root.goToFolder"></li>
</ul>
<br>
<div class="mails" data-bind="with: chosenFolderData">
<p><label>From</label>: <span data-bind="text: name"></span></p>
</div>
<script type="text/javascript">
function WebmailViewModel() {
var self = this;
self.folders = ko.observableArray(['cardio', 'classic', 'mass', 'abs']);
self.chosenFolderId = ko.observable();
self.chosenFolderData = ko.observable();
self.chosenMailData = ko.observable();
// Behaviours
self.goToFolder = function(folder) {
console.log(folder);
self.chosenFolderId(folder);
self.chosenMailData(null);
//apply you ajax call and fill 'chosenFolderData'
self.chosenFolderData({"id":"1", "name":"cardio", "image":"aslas", "header":"Kardio", "message":"message\r\n"});
};
self.goToFolder('Inbox');
};
ko.applyBindings(new WebmailViewModel());
</script>
<ul class="folders" data-bind="foreach: folders">
<li data-bind="text: $data,
css: { selected: $data == $root.chosenFolderId() },
click: $root.goToFolder"></li>
</ul>
<br>
<div class="mails" data-bind="with: chosenFolderData">
<p><label>From</label>: <span data-bind="text: name"></span></p>
</div>
<script type="text/javascript">
function WebmailViewModel() {
var self = this;
self.folders = ko.observableArray(['cardio', 'classic', 'mass', 'abs']);
self.chosenFolderId = ko.observable();
self.chosenFolderData = ko.observable();
self.chosenMailData = ko.observable();
// Behaviours
self.goToFolder = function(folder) {
console.log(folder);
self.chosenFolderId(folder);
self.chosenMailData(null);
//apply you ajax call and fill 'chosenFolderData'
self.chosenFolderData({"id":"1", "name":"cardio", "image":"aslas", "header":"Kardio", "message":"message\r\n"});
};
self.goToFolder('Inbox');
};
ko.applyBindings(new WebmailViewModel());
</script>