Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week 5 project #6

Open
wants to merge 5 commits into
base: week-5-project
Choose a base branch
from
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
11 changes: 10 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@

<!-- Put your html here -->

<div class="stocks"></div>

<!-- Using {{}} instead of <%=%> to avoid conflict with erb -->

<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>
<script src="vendor/underscore.js" type="text/javascript"></script>
<script src="vendor/backbone.js" type="text/javascript"></script>

<!-- Globals -->
<script src="src/util.js" type="text/javascript"></script>
<!-- Models -->
Expand Down
1 change: 0 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

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

window.updateStocks = function (data) {

Expand Down
12 changes: 9 additions & 3 deletions src/models/stock.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
(function () {
// The parseFloat() function converts the incoming newPrice from a string to floating integer.
//
//
// Sets the new price value on the model

(function(){

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
20 changes: 17 additions & 3 deletions src/views/stock-view.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
// Listen for StockView's model's changes in initialize.
//
// Render function updates the 'el'ement using an underscore template.
//
// Render the stock name and price.

(function () {

window.StockView = Backbone.View.extend({
className: 'stock'
// TODO
});
className: 'stock',
initialize: function(){
this.listenTo(this.model, 'change:price', this.render);
},

render: function(){
var priceTemplateHtml = $('#templates .stock').html();
var priceTemplate = _.template(priceTemplateHtml);
var newTemplateHtml = priceTemplate({ name: this.model.get('name'), price: this.model.get('price') });
$(this.el).html(newTemplateHtml);
}
});
})();