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

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 0.83 KB  |  hits: 4  |  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. $(this.el).find() works in event handler, not in initialize function (backbone.js)
  2. PhotoListItemView = Backbone.View.extend({
  3.     tagNAme: 'div',
  4.     className: 'photo_box',
  5.  
  6.     events: {
  7.         'click': 'setStateLike'
  8.     },
  9.  
  10.     initialize: function() {
  11.         this.setStateLike();
  12.     },
  13.  
  14.     render: function() {
  15.         $(this.el).html( this.template( this.model.toJSON() ) );
  16.         return this;
  17.     },
  18.  
  19.     setStateLike: function() {
  20.         console.log( $(this.el).find('#like') );  // returns []
  21.         if(this.model.get('is_liked')) {
  22.             console.log( $(this.el) );        // returns correctly
  23.             console.log( $(this.el).find('#like') );  // returns []
  24.             // Change icon to Active state
  25.             $(this.el).find('#like.photo_btn').addClass('photo_btn_active').attr('id', 'unlike');
  26.         }
  27.     }
  28. });