Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e55ba50

Browse files
committed
knockout demo
1 parent 11a66fe commit e55ba50

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

demo/knockout.html

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,15 @@
77
<title>Knockout.js demo</title>
88

99
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
10-
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.2.2/gridstack.min.js"/>
11-
1210
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css"/>
13-
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.2.1/gridstack.min.css"/>
14-
15-
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
16-
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
17-
<!--[if lt IE 9]>
18-
<script src="//oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
19-
<script src="//oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
20-
<![endif]-->
11+
<link rel="stylesheet" href="../gridstack.css"/>
2112

2213
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
2314
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui.js"></script>
24-
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
2515
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
2616
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
2717
<script src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
28-
<script src="//cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.js"></script>
29-
<script src="//cdnjs.cloudflare.com/ajax/libs/gridstack.js/0.2.2/gridstack.min.js"></script>
18+
<script src="../gridstack.js"></script>
3019

3120
<style type="text/css">
3221
.grid-stack {
@@ -44,6 +33,12 @@
4433
<div class="container-fluid">
4534
<h1>knockout.js Demo</h1>
4635

36+
<div>
37+
<button data-bind="click: add_new_widget">Add new widget</button>
38+
</div>
39+
40+
<br>
41+
4742
<div data-bind="component: {name: 'dashboard-grid', params: $data}"></div>
4843
</div>
4944

@@ -58,22 +53,17 @@ <h1>knockout.js Demo</h1>
5853
this.widgets = controller.widgets;
5954

6055
this.afterAddWidget = function (items) {
61-
_.each(items, function (item) {
62-
item = $(item);
63-
if (item.data('_gridstack_node') || item[0].nodeType != 1)
64-
return;
65-
66-
if (grid == null) {
67-
grid = $(componentInfo.element).find('.grid-stack').gridstack({
68-
auto: false
69-
}).data('gridstack');
70-
}
71-
72-
grid.add_widget(item);
73-
ko.utils.domNodeDisposal.addDisposeCallback(item[0], function () {
74-
grid.remove_widget(item);
75-
});
76-
}, this);
56+
if (grid == null) {
57+
grid = $(componentInfo.element).find('.grid-stack').gridstack({
58+
auto: false
59+
}).data('gridstack');
60+
}
61+
62+
var item = _.find(items, function (i) { return i.nodeType == 1 });
63+
grid.add_widget(item);
64+
ko.utils.domNodeDisposal.addDisposeCallback(item, function () {
65+
grid.remove_widget(item);
66+
});
7767
};
7868
};
7969

@@ -83,16 +73,32 @@ <h1>knockout.js Demo</h1>
8373
template:
8474
[
8575
'<div class="grid-stack" data-bind="foreach: {data: widgets, afterRender: afterAddWidget}">',
86-
' <div class="grid-stack-item" data-bind="attr: {\'data-gs-x\': $data.x, \'data-gs-y\': $data.y, \'data-gs-width\': $data.width, \'data-gs-height\': $data.height}">',
87-
' <div class="grid-stack-item-content" data-bind="text: $index"></div>',
76+
' <div class="grid-stack-item" data-bind="attr: {\'data-gs-x\': $data.x, \'data-gs-y\': $data.y, \'data-gs-width\': $data.width, \'data-gs-height\': $data.height, \'data-gs-auto-position\': $data.auto_position}">',
77+
' <div class="grid-stack-item-content"><button data-bind="click: $root.delete_widget">Delete me</button></div>',
8878
' </div>',
89-
'</div>'
90-
].join('\n')
79+
'</div> '
80+
].join('')
9181
});
9282

9383
$(function () {
9484
var Controller = function (widgets) {
85+
var self = this;
86+
9587
this.widgets = ko.observableArray(widgets);
88+
89+
this.add_new_widget = function () {
90+
this.widgets.push({
91+
x: 0,
92+
y: 0,
93+
width: Math.floor(1 + 3 * Math.random()),
94+
height: Math.floor(1 + 3 * Math.random()),
95+
auto_position: true
96+
});
97+
};
98+
99+
this.delete_widget = function (item) {
100+
self.widgets.remove(item);
101+
};
96102
};
97103

98104
var widgets = [
@@ -102,7 +108,8 @@ <h1>knockout.js Demo</h1>
102108
{x: 1, y: 2, width: 4, height: 2}
103109
];
104110

105-
ko.applyBindings(new Controller(widgets));
111+
var controller = new Controller(widgets);
112+
ko.applyBindings(controller);
106113
});
107114
</script>
108115
</body>

0 commit comments

Comments
 (0)