I am attempting to fire a function that is part of the data object I'm passing to RactiveJS when populating a template. I've tried a few other libraries, but I can't find what I'm looking for. Here is the description of what I'm doing, and I'm hopeful that someone can suggest a library for doing what I'm trying to do.
A service is feeding my application a JSON Object that looks something like this
var data = {
"users": {
"Aaron": {
"age": "31",
"level": "legit",
"talk": function(){
console.log("Howdy! I'm " + this.level);
}
},
"Pete": {
"age": "30",
"level": "godlike",
"talk": function(){
console.log("Howdy! I'm " + this.level);
}
}
}
};
My template appears to be correct As instructed by the Documentation
var usersTemplate = "<ul>{{#users:name}}<li><a href='#' on-click='talk'>{{name}} says:</li>{{/users}}</ul>";
Then I build a new Ractive instance
var people = new Ractive({
el: "#userlist",
template: usersTemplate ,
data: data
});
Trouble is, clicking on each user won't actually do anything because RactiveJS doesn't seem to work like that. It handles events like this.
people.on({
talk: function(evt){
evt.original.preventDefault();
console.log("I clicked on the template");
}
});
What I'm trying to do is trigger the action
function inside of the data object that I passed in without the controller knowing what the name of the method is. Specifying it in the template, and in the data object should be enough.
Ractive gives enough information that I can traverse the data object, but this isn't good enough.
people.on({
talk: function( evt ) {
evt.original.preventDefault();
var pointer = json_output;
var path = evt.keypath.split(".");
for( var i = 0; i < path.length; i++){
console.log(i);
pointer = pointer[path[i]];
}
if(pointer.hasOwnProperty("action") && typeof(pointer.action) === "function") {
pointer.action(evt);
}
}
});
Is RactiveJS not the right library for the job? Thank you ahead of time for helping me in my search.