I have this model:
(function(app) {
app.productLine =
ng.core.Component({
selector: 'product-line',
templateUrl: 'templates/sales-product.html',
})
.Class({
constructor: function() {
this.products = [new app.product('101010101010101', '1', '19.99')];
},
addLine: function() {
this.products.push(new app.product('101010101010101', '1', '19.99'));
}
});
})(window.app || (window.app = {}));
(function(app) {
app.product = function (upc, quantity, price) {
this.upc = upc;
this.quantity = quantity;
this.price = price;
return this;
}
})(window.app || (window.app = {}));
However, I can't figure out how to expose addLine() so I can call it elsewhere.
Logging productLine only shows the constructor:
console.log(app.productLine);
function app.productLine<.constructor()
And calling app.productLine.addLine() gives TypeError: app.productLine.addLine is not a function.
EDIT:
I found that adding the addLine function to app.productLine directly does work. Of course, then the scope of this is changed, so there needs to be a reference to the constructor's results in a more obvious location.
(function(app) {
app.productLine =
ng.core.Component({
selector: 'product-line',
templateUrl: 'templates/sales-product.html',
})
.Class({
constructor: function () {
this.products = [
{ upc: '',
quantity: '',
price: ''
}
];
app.productLine.products = this.products;
}
});
app.productLine.add = function (upc, quantity, price) {
app.productLine.products.push({
upc: upc,
quantity: quantity,
price: price
});
}
})(window.app || (window.app = {}));
You can then run app.productLine.add(123,456,789); and the model is updated.
The view, however, is not updated immediately. I believe it is necessary to trigger an update somehow, but with 2-way data binding, if you use the UI to update the model all of the updates appear.