/
index.html
124 lines (108 loc) · 2.72 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<html ng-app="myApp">
<head>
<title>Sugo</title>
<script src="bower_components/angular/angular.min.js"></script>
<script src="src/sugo.js"></script>
<script src="bower_components/underscore/underscore-min.js"></script>
</head>
<body ng-controller="MainController as main">
<h1>{{main.productModel.name()}}</h1>
<ul>
<li ng-repeat="product in main.products">
{{product.title}} - {{product.created_at | date:"medium"}}
<button ng-click="product.delete()">delete</button>
</li>
</ul>
<button ng-click="main.addProduct()">Add product</button>
<h1>{{main.second.name()}}</h1>
<input type="text" ng-model="productSearch" ng-init="productSearch = 'Product #'">
<button ng-click="main.search(productSearch)">Search by title</button>
<h4>Founded: {{ main.founded.toString({title:1}) }}</h4>
</body>
</html>
<script>
(function(){
var app = angular.module('myApp', ['sugo']);
app.controller('MainController', ['$scope','$sugo', function($scope, $sugo){
var _this = this;
_this.scope = $scope;
_this.products = {};
_this.addProduct = _addProduct;
_this.search = _search;
_this.founded = undefined;
var VariantSchema = new $sugo.schema({
title : String,
color: String,
quantity: Number
});
/** New Product Schema */
var ProductSchema = new $sugo.schema({
title: String,
/** TODO: variants: [VariantSchema] */
});
// define model
_this.productModel = $sugo.model('Product', ProductSchema);
//_this.variantsModel = $sugo.model('Variants', VariantSchema);
//init data
_getAll();
/** Add new element from instance */
// create instance
var element = _this.productModel.new({title: 'Hello World'});
// save into collection after a while
setTimeout(function(){
element.save();
_this.scope.$apply();
}, 2000);
/**
* Add product with a insert query
*/
function _addProduct(){
_this
.productModel
.insert({
title: "Product #" + Object.keys(_this.products).length,
listings:[]
})
.then(
_getAll,
function(error){
console.error(error);
});
}
/**
* Get all products
* @return {[type]} [description]
*/
function _getAll(){
_this
.productModel
.find()
.then(
function(result){
_this.products = result;
},
function(error){
console.error(error);
});
}
/**
* Find element with a query
* @param {[type]} index [description]
* @return {[type]} [description]
*/
function _search(title){
_this
.productModel
.findOne({title: title})
.then(
function(result){
_this.founded = result;
result.save();
},
function(error){
_this.founded = undefined;
})
}
}]);
})(window);
</script>