chap2 -> Creating templates -> The catalog template -> part 2

Move logic of determine color into product.js

Note: This change is not completed
chap2
Jason Zhu 2022-04-19 22:55:51 +10:00
parent 3001e6f436
commit d18a1efd31
2 changed files with 5 additions and 1 deletions

View File

@ -61,7 +61,7 @@
</tr> </tr>
</thead> </thead>
<tbody data-bind="foreach:catalog"> <tbody data-bind="foreach:catalog">
<tr data-bind="style:{color:stock() < 5 ? 'red' : 'black'}"> <tr data-bind="style:lineColor">
<td data-bind="text:name"></td> <td data-bind="text:name"></td>
<td data-bind="text:price"></td> <td data-bind="text:price"></td>
<td data-bind="text:stock"></td> <td data-bind="text:stock"></td>

View File

@ -5,10 +5,14 @@ var Product = function (id, name, price, stock) {
_name = ko.observable(name), _name = ko.observable(name),
_price = ko.observable(price), _price = ko.observable(price),
_stock = ko.observable(stock); _stock = ko.observable(stock);
var _lineColor = ko.computed(function() {
return (_stock() < 5) ? 'red' : 'black';
});
return { return {
id: _id, id: _id,
name: _name, name: _name,
price: _price, price: _price,
stock: _stock, stock: _stock,
lineColor: _lineColor
}; };
}; };