Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
</head>
<body>

<!-- Put your html here -->
<div class="stocks"></div>

<!-- Put your html here -->
<div id="templates">
<script class="stock" type="text/template">
<h1>{{ name }}</h1>
<p>{{ price }}</p>
</script>
</div>

<!-- 3rd-party scripts -->
<script src="vendor/jquery-1.10.2.js" type="text/javascript"></script>
Expand Down
9 changes: 5 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* main.js
*
*/
(function () {

// Create our stocks
Expand All @@ -11,15 +15,12 @@

// Create a view for each stock and render its element to the page
_.each(stocks, function (stock) {
var view = new StockView({ model: stock });
var view = new StockView({ model: stock }); /* read */
view.render();
$('.stocks').append(view.el);
});

// ----
// Get realtime stock data
// It's not required that you understand the code.

window.updateStocks = function (data) {

_.each(data.query.results.quote, function (quote) {
Expand Down
18 changes: 16 additions & 2 deletions src/models/stock.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
/*
* stock.js
*
*/

(function () {

//Backbone Model
/*
Backbone.Model.prototype.setByName = function(key, value, options) {
var setter = {};
setter[key] = value;
this.set(setter, options);
};
*/
window.Stock = Backbone.Model.extend({
updatePrice: function (newPrice) {
console.log('Updating', this.get('name'), 'price to:', newPrice);
// TODO
this.price = parseFloat(newPrice);
this.set({ price: this.price });
console.log('Updating', this.get('name'), 'price to:', this.get('price'));
}
});

Expand Down
19 changes: 17 additions & 2 deletions src/views/stock-view.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
/*
* stock-view.js
*
*/

(function () {

window.StockView = Backbone.View.extend({
className: 'stock'
// TODO
className: 'stock',
initialize: function () {
this.listenTo(this.model, 'change:price', this.render );
//console.log('New price for', this.model.get('name'), this.model.get('price'));
},
render: function () {
var priceTemplateHtml = $('#templates .stock').html();
var priceTemplate = _.template(priceTemplateHtml);
var newHtml = priceTemplate({ name: this.model.get('name'), price: this.model.get('price') });
//console.log(newHtml);
$(this.el).html(newHtml);
}
});

})();