|
|
@@ -1,3 +1,4 @@
|
|
|
+var BASE_URL = 'http://192.168.1.127/api/';
|
|
|
function http(url, params) {
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
httpRequest = new XMLHttpRequest();
|
|
|
@@ -5,7 +6,7 @@ function http(url, params) {
|
|
|
httpRequest.onreadystatechange = function() {
|
|
|
if (httpRequest.readyState == XMLHttpRequest.DONE ) {
|
|
|
if (httpRequest.status == 200) {
|
|
|
- resolve(httpRequest.responseText);
|
|
|
+ resolve(JSON.parse(httpRequest.responseText));
|
|
|
} else {
|
|
|
reject("pls fix later");
|
|
|
}
|
|
|
@@ -19,36 +20,81 @@ function http(url, params) {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-http('http://192.168.1.127/api/products/category/58caba2ce53f0644597b74e8').then(function(data) {
|
|
|
- //console.log("2017 no frameworks", data);
|
|
|
+http('http://192.168.1.127/api/products/listcategories').then(function(data) {
|
|
|
+
|
|
|
}, function(err){
|
|
|
- console.log("err",err);
|
|
|
+ console.log("err", err);
|
|
|
});
|
|
|
|
|
|
-
|
|
|
+function logError (err) {
|
|
|
+ console.log('Error: ', err);
|
|
|
+}
|
|
|
var sections = [
|
|
|
'empresa',
|
|
|
'productos'
|
|
|
];
|
|
|
|
|
|
+function initProducts(category) {
|
|
|
+ http(BASE_URL + 'products/category/' + category).then(function (products) {
|
|
|
+ products.forEach(function (product) {
|
|
|
+ productsListed_.push(product);
|
|
|
+ })
|
|
|
+ }, logError)
|
|
|
+}
|
|
|
+
|
|
|
var selectedSection_ = {
|
|
|
- empresa: true,
|
|
|
- productos: false
|
|
|
- };
|
|
|
+ empresa: true,
|
|
|
+ productos: false
|
|
|
+};
|
|
|
|
|
|
+var productsListed_ = [];
|
|
|
rivets.configure({
|
|
|
executeFunctions: true
|
|
|
})
|
|
|
|
|
|
+function getCategories() {
|
|
|
+ if(productCategories_.length === 0) {
|
|
|
+ http(BASE_URL + 'products/listcategories').then(function (data) {
|
|
|
+ data.forEach(function (category) {
|
|
|
+ productCategories_.push(category)
|
|
|
+ })
|
|
|
+ }, logError)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+var productCategories_ = [];
|
|
|
+
|
|
|
var app = document.getElementById('app');
|
|
|
+
|
|
|
+rivets.binders['src-strict'] = function(el, value) {
|
|
|
+ var img = new Image()
|
|
|
+
|
|
|
+ img.onload = function() {
|
|
|
+ $(el).attr('src', value)
|
|
|
+ }
|
|
|
+
|
|
|
+ img.src = value
|
|
|
+}
|
|
|
+
|
|
|
var view = rivets.bind(app, {
|
|
|
sections: sections,
|
|
|
selectedSection: selectedSection_,
|
|
|
+ productCategories: productCategories_,
|
|
|
+ productsListed: productsListed_,
|
|
|
selectSection: function (event, scope) {
|
|
|
var newValue = event.target.text;
|
|
|
window.history.pushState({}, newValue, '#' + newValue)
|
|
|
chooseSection(newValue);
|
|
|
event.target.className = 'selected';
|
|
|
+ scope.getData[newValue]();
|
|
|
+ },
|
|
|
+ selectCategory: function (event, scope) {
|
|
|
+ var category = productCategories_.find(R.propEq('name', event.target.text));
|
|
|
+ initProducts(category._id)
|
|
|
+ },
|
|
|
+ getData: {
|
|
|
+ productos: getCategories,
|
|
|
+ empresa: function(){}
|
|
|
}
|
|
|
});
|
|
|
|