I am trying to call a function but I get an error stating function undefined.
window.PackageView = Backbone.View.extend({
开发者_如何学运维 tagName: "div",
className: "package-template",
events:{
"click #display-name" : "getNodeId",
},
initialize: function() {
_.bindAll(this,'render', 'getNodeId', 'getAction');
this.template = _.template($('#package-template').html());
$(this.el).html(this.template); //Load the compiled HTML into the Backbone "el"
nodeInstance.bind('reset', this.render, this);
},
render: function() {
//.. no need to worry about this.
},
getNodeId: function(){
objectJSON = jAccess.getJSON(); // I get the JSON successfully
nodeIdArray = []
_.each(objectJSON, function(action){
_.each(action.Nodes, function(action){
nodeIdArray.push(action.Id);
this.getAction(); // Here is the problem. I get an error
});
});
},
getAction: function(){
actionsArray = [];
objectJSON = jAccess.getJSON();
_.each(objectJSON, function(action){
_.each(action.Nodes, function(action){
if(action.Id == 5) {
_.each(action.Actions, function(action){
actionsArray.push(action.Name);
});
}
});
});
console.log(actionsArray);
}
});
I don't know where I am going wrong here. Help me out.
I get an error saying 'undefined' is not a function evaluating ('this.getAction()')
As a simple fix, you should be able to do:
getNodeId: function(){
var self = this;
objectJSON = jAccess.getJSON();
nodeIdArray = []
_.each(objectJSON, function(action){
_.each(action.Nodes, function(action){
nodeIdArray.push(action.Id);
self.getAction();
});
});
},
I think there might be a more elegant solution using underscores bind function, I'll have a check.
EDIT The more elegant solution is only usable when using the each method on a collection from a RESTful resource, so does not apply in this case. For example if you have a collection of messages, you could do the following to keep context.
addAllMessages: function(messages) {
messages.each(this.addMessage, this);
},
精彩评论