-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Receive arguments to binder #258
Comments
Which arguments? I don't understand the question ^^ |
Update the post with proper markdown so that the examples are available :) |
You need to get the params from the element: var params = element.getAttribute("rv-params"); Or more precisely var params = element.getAttribute(this.view.config.prefix + "-params"); |
Yupp but the problem that way would be to get the actual value of model.id id like to take advantage of model bindings here. |
Well, you don't have any reference to the model... I don't think that a function call with arguments should be notated inside of html. It gets way to complicated and requires evaluating of inline JavaScript. Reminds me of the good old times of Imho it's better to bind a function that already has access to the model's reference. Your controller should know the model. |
I was just thinking perhaps someone else would have already solved how to use their frontend frameworks reverse url lookup solved with bindings :) all i care for is to set the href dynamically without needing to much hassle |
You're trying to generate the <a rv-href="myModel" route="blog/:blogId/post/:postId/edit" ></a> and then: rivets.binders.href = function (el, value) {
var route = el.getAttribute("route");
var href = generateHref(route, value);
// assuming that generateHref() turns
// blog/:blogId/post/:postId/edit into blog/2/post/3/edit
el.setAttribute("href", href);
}; ? |
@jhnns Nice one. 👍 |
This should be a question similar to source one. I have a wizard form and want to implement transition between steps using Here I have a lot of options, and I can't understand which one is correct.
rivets.binders.changestep =
bind: (el) ->
@args = ["click"]
rivets.binders['on-*'].routine.call @, @el, =>
@model.set "step", el.getAttribute("rv-change-step")
unbind: -> rivets.binders['on-*'].unbind.call @, @el <div rv-change-step="2"> But - isn't it too complex? Source task is dead simple - call function with argument on click - and I need some incomprehensible workaround, need new binders to do it?
<div rv-on-click="model.setStep | partial 2"> But this also means that new function will be created on each click. Which is not good, of course, so this solution looks more like a hack.
<div rv-on-click="model.goToStep2"> Methods can be auto-generated, so it will be not so much code. But this is like some kind of china-code, as for me.... Please help me to understand which path is right. |
I have a fourth solution 😀 : rivets.configure({
/**
* The base event handler when working with dom events (e.g. when using bind-on-click).
*
* The handler looks for the presence of a data-[eventType]-attribute that specifies which argument should be
* passed to the event listener. If no argument is specified, the original dom event and the binding is passed.
*
* The event listener is finally called on the view (which usually contains all event listeners).
*
* @param {Element} target
* @param {Event} event
* @param {rivets.Binding} binding
*/
handler: function (target, event, binding) {
var eventType = binding.args[0];
var arg;
if (target.dataset && (arg = target.dataset[eventType])) {
this.call(binding.view.models.self, binding.view.models[arg] || arg);
} else {
// that's rivets' default behavior afaik
this.call(binding.view.models.self, event, binding);
}
}
}); Then you can pass custom arguments to any event handler: <a rv-on-click="model.goToStep" data-on-click="2"> The argument will be Be careful: In my implementation the value of var myView = {
"2": "Two"
}; Will pass |
For anyone wondering, the good implementation of the config handler would be: var rivets = require('rivets');
rivets.configure({
// extracted from: https://github.com/mikeric/rivets/issues/258#issuecomment-52489864
// This configuration allows for on- handlers to receive arguments
// so that you can onclick="steps.go" data-on-click="share"
handler: function (target, event, binding) {
var eventType = binding.args[0];
var arg = target.getAttribute('data-on-' + eventType);
if (arg) {
this.call(binding.model, arg);
} else {
// that's rivets' default behavior afaik
this.call(binding.model, event, binding);
}
}
}); <a rv-on-click="steps.go" data-on-click="homePage">home</a> Previous code was not working, surely because of changes in rivets. |
Hi
I cant figure this one out, perhaps its a documentation issue. id like to do an utility helper that utilizes my frameworks url reversal methods (similar to these in django and rails)
Example:
Thanks
The text was updated successfully, but these errors were encountered: