Created
August 21, 2014 11:38
-
-
Save ascott1/03158e4b0d72fc4a7402 to your computer and use it in GitHub Desktop.
OAH Backbone Simple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(function(){ | |
| // create a loan model | |
| var Loan = Backbone.Model.extend({ | |
| defaults: { | |
| title: 'Loan A', | |
| amount: 200000, | |
| downpayment: 20000, | |
| credit: 720, | |
| term: 30 | |
| } | |
| }); | |
| // create a collection of loans | |
| var LoanList = Backbone.Collection.extend({ | |
| model: Loan | |
| }); | |
| // prefill a loan | |
| var loans = new LoanList([ | |
| new Loan({title: 'Loan A'}), | |
| new Loan({title: 'Loan B', amount: 240000, downpayment: 15000}), | |
| new Loan({title: 'Loan C', amount: 240000, downpayment: 15000}) | |
| ]); | |
| var LoanView = Backbone.View.extend({ | |
| tagName: 'li', | |
| events: { | |
| 'change input': 'contentChanged', | |
| }, | |
| initialize: function(){ | |
| this.total = $('.total'); | |
| this.listenTo(this.model, 'change', this.render); | |
| }, | |
| contentChanged: function(e) { | |
| var field = $(e.currentTarget); | |
| var data = {}; | |
| data[field.attr('class')] = field.val(); | |
| this.model.set(data); | |
| }, | |
| render: function(){ | |
| var total = this.model.get('amount') - this.model.get('downpayment'); | |
| // make some HTML | |
| this.$el.html( | |
| '<h2>' + this.model.get('title') + '</h2>' + | |
| '<label>Amount</label> <br>'+ | |
| '<input class="amount" value="' + this.model.get('amount') + '" name="' + this.model.get('title') + '-amount" /> <br>' + | |
| '<label>Down Payment</label><br>' + | |
| '<input class="downpayment" value="' + this.model.get('downpayment') + '" name="' + this.model.get('title') + '-dp" /> <br>' + | |
| '<p>total: <strong class="total">$' + total + '</strong></p>' | |
| ); | |
| // return it | |
| return this; | |
| } | |
| }); | |
| // main app view | |
| var App = Backbone.View.extend({ | |
| el: $('#main'), | |
| initialize: function() { | |
| this.list = $('#loans'); | |
| loans.each(function(loan){ | |
| var view = new LoanView({model: loan}); | |
| this.list.append(view.render().el); | |
| }, this); | |
| }, | |
| render: function(){ | |
| var total = 0; | |
| return this; | |
| } | |
| }); | |
| new App(); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <title>OAH Simple Backbone Demo</title> | |
| <style> | |
| .wrap { | |
| max-width: 960px; | |
| margin: 0 auto; | |
| } | |
| ul, | |
| li { | |
| margin: 0; | |
| padding: 0; | |
| list-style: none; | |
| } | |
| li { | |
| display: inline-block; | |
| width: 30%; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div id="main" class="wrap"> | |
| <h1>Loanz</h1> | |
| <ul id="loans"> | |
| </ul> | |
| </div> | |
| <!-- JavaScript Includes --> | |
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script> | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script> | |
| <script src="app.js"></script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment