Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 0.86 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. how to access a models data from a view in backbone.js
  2. var person = Backbone.Model.extend({
  3.       initialize: function(){
  4.         console.log('cool');
  5.       },
  6.       defaults:{
  7.           names:['a','k','d','s','h','t']
  8.       }
  9.     })
  10.        
  11. var person_view = Backbone.View.extend({
  12.    model : person,
  13.    output: function(){
  14.       console.log(this.model.get('names'))
  15.    }
  16. });
  17.        
  18. var obj = new person_view()
  19.        
  20. obj.output()
  21.        
  22. TypeError: Object function (){ parent.apply(this, arguments); } has no method 'get'
  23.        
  24. var person_view = Backbone.View.extend({
  25.     initialize: function() {
  26.         this.model = new person();
  27.     },
  28.     output: function(){
  29.         console.log(this.model.get('names'))
  30.     }
  31. });
  32.        
  33. var person_view = Backbone.View.extend({
  34.   output: function(){
  35.     console.log(this.model.get('names'))
  36.   }
  37. });
  38.  
  39. var obj = new person_view({
  40.   model : new person()
  41. });