2012-08-22 1 views
0
var ListView = Backbone.View.extend({ 
el: $('hello'), 
events: {       
     'click .myName': 'namefunc', 
}, 
initialize: function() { 
    var stuff = new FieldCollection(); 
    stuff.parse(); 
    var self = this; 
    stuff.fetch({ 
     success: function (collection, response) { 
      console.log(response); 
      self.render(response); 
      return response; 
     } 
    }); 
}, 
render:function(output){ 
    _.each(output, function(i){ 
     p=i.name; 
     $(this.el).append("<button class='myName'>"+p+"</button><h1>"+i.img+"</h1><br/>"); 
    },this);  

}, 
namefunc:function(){ 
    alert('hiii'); 
} 

comment lier le changement la sortie hiii d'alerte avec les détails de la personne contre laquelle le bouton est pressé ...épine dorsale + fixation

{ 
    "name": "john", 
    "img": "john.jpg", 
    "loc": "delhi", 
    "msg": "hello there" 
} 

c'est le JSON qui im ​​obtenir .. ..when je clique sur le bouton (ayant le nom john), il doit montrer son msg ....

+2

Pouvez-vous expliquer mieux la question? – fguillen

+0

Découvrez ce [post] [1]. Tout ce que vous avez besoin [1]: http://stackoverflow.com/questions/5270188/backbone-js-event-binding – schacki

+0

@fguillen vérifier maintenant –

Répondre

1
initialize: function() { 
var self = this; 
_.bindAll(this, 'render'); 
this.collection = new FieldCollection(); 
this.collection.parse(); 

this.collection.fetch({ 
    success: function (collection, response) { 
     console.log(response); 
     self.render(); 
    } 
}); 

},

render:function(){ 
var self = this; 
_.each(this.collection, function(model) { 
    // here you should create subviews and delegate events within particular view 
    // i.e: 
    // self.buttonViews.push(new ButtonView({ model: model})); 
    // but for now you can use data-id attribute to match the button you clicked 
    $(self.el).append("<button class='myName' data-id=" + model.id + ">" + model.name + "</button><h1>" + model.img + "</h1><br/>"); 
});  

},

namefunc:function(){ 
//use data-id attribute we put in button tag earlier to catch the id 
//and use this.collection.get('id') function to get the model you want to get parameters of 

}

1

Si je l'ai bien compris votre question, vous pourriez faire quelque chose comme ...

var _self = this, 
    obj = Object; 

$("<button>"+someVariable+"</button>") 
    .addClass('myName') 
    .appendTo(this.$el) 
    .on({ 
     "click": function() { _self.fn(obj) } 
    }); 

+0

aussi, dans l'épine dorsale vous avez un objet jquery pour this.el automatiquement dans ce. $ El donc vous n'avez pas besoin de $ (this.el) –

+0

non il n'a pas résolu mon problème –

Questions connexes