|
@@ -4,13 +4,26 @@
|
4
|
4
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.1.6/zepto.min.js"></script>
|
5
|
5
|
<script src="../lib/php_crud_api_transform.js"></script>
|
6
|
6
|
<script>
|
7
|
|
-$(function(){
|
8
|
|
- var url = 'http://localhost/api.php/posts';
|
9
|
|
- $.post(url, {user_id:1,category_id:1,content:"from knockout"});
|
10
|
|
- $.get(url, function(data) {
|
11
|
|
- $('#myApplication').html('<ul data-bind="foreach: posts"><li><span data-bind="text: id"></span>, <span data-bind="text: content"></span></li></ul>')
|
12
|
|
- ko.applyBindings(php_crud_api_transform(data),$('#myApplication')[0]);
|
13
|
|
- });
|
|
7
|
+var url = '../api.php/posts';
|
|
8
|
+function PostList(){
|
|
9
|
+ var self = this;
|
|
10
|
+ self.posts = ko.observableArray([]);
|
|
11
|
+ self.set = function(array) {
|
|
12
|
+ self.posts.removeAll();
|
|
13
|
+ for (i=0;i<array.length;i++) {
|
|
14
|
+ self.posts.push(array[i]);
|
|
15
|
+ }
|
|
16
|
+ };
|
|
17
|
+ self.update = function() {
|
|
18
|
+ $.get(url, function(data) {
|
|
19
|
+ self.set(php_crud_api_transform(data).posts);
|
|
20
|
+ });
|
|
21
|
+ };
|
|
22
|
+ $.post(url, {user_id:1,category_id:1,content:"from knockout"}, self.update.bind(self));
|
|
23
|
+};
|
|
24
|
+$(function(){
|
|
25
|
+ $('#myApplication').html('<ul data-bind="foreach: posts"><li><span data-bind="text: id"></span>, <span data-bind="text: content"></span></li></ul>');
|
|
26
|
+ ko.applyBindings(new PostList(),$('#myApplication')[0]);
|
14
|
27
|
});
|
15
|
28
|
</script>
|
16
|
29
|
</head>
|