-
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
rv-show and rv-each #366
Comments
Something similar to the following perhaps? :
Where |
I think this is already possible with the <li rv-show="initItem" rv-each-todo="data.myTodos">
{todo.text}
</li> (wrong example, see below) initItem: function (event, models) {
controller.showMe(models.data.myTodos[models.index]);
} |
Thanks for your reply. |
Oops, sorry, my example is wrong. It should not be passed to <li rv-constructor="initItem" rv-each-todo="data.myTodos">
{todo.text}
</li>
|
But what if I want to pass the item to my controller, do some sort of calculation or procedure and then show the item depending that result. That would make sense to have it called from the |
When using data-binding imho you should not run procedures when reading values. Usually you should run procedures when something changes. Reading values does in fact happen very often and thus has an impact on performance. The only exception to this rule are formatters which are necessary to bring the raw data in a presentable state. |
Yes a complicated procedure is perhaps not the best example for this, due to as you say - performance. But the ability to evaluate the item in question more thoroughly would/could be very handy. For example you may want to filter out and only show a certain todo on a certain day if the todo text says "hello" for a crude example. Such logic would be great client side in a controller. It's a bit like like a jQuery event that has the event and element in question passed as an argument. What the developer does with that data and it's performance is no longer a framework problem but that of the developer. |
But I'd use a view model for that case. For example: var todos = [
{ label: "Clean kitchen", done: false },
{ label: "Clean bathroom", done: false }
]; Then I'd pass them to a function which creates view models // createViewModels() is now applying all the logic
var viewTodos = createViewModels(todos);
[
{ label: "Clean kitchen", done: false, visible: false },
{ label: "Clean bathroom", done: false, visible: true }
]; And now let's pass our view model to rivets rivets(element, viewTodos); And if anything changes (for example the user adds a filter), you just need to manipulate your view models. This way your logic isn't executed when reading the values. |
Yea that could be done too. But I don't know, it feels kind of messy of somehow :) What if, you get a json string from the DB for a form for instance, show the form, allow the user to manipulate the data and send the json object back to the server as it is. The server simply takes the json and saves it to the database, with you example there would be extra values in the object that are purely for the frontend. It would require you to remove them again before sending the data. Plus you would need to unbind the data when manipulating it before submit in order to stop the view updating. It just seems a bit like a hack, placing the data in the model and then removing it etc. Also, what if the |
If you don't want to mess with the model itself you can also namespace it: var todoItem = {
visible: false,
data: {
label: "Clean kitchen",
done: false
}
} In my experience you'll always need a separate view model in any advanced app. It's also a widely accepted software pattern, so this doesn't feel hacky to me. |
You can also leverage prototype inheritance: viewModel = Object.create(model);
viewModel.visible = false;
console.log(viewModel.label); // "Clean kitchen"
console.log(viewModel.visible); // false
console.log(model.visible); // undefined You'll need to save the changes back to the model before sending it as JSON to the server: Object.keys(model).forEach(function (key) {
model[key] = viewModel[key];
}); |
Thanks for your input jhnns, I get where you are coming from... but it just seems like an overkill solution for me :) It would be just be much easier to pass to the object as an argument when a controller function is called :) |
It would be good if the current iterated item is passed to the rv-show call, so that you can determine whether to show the item within a controller function. However, no arguments are passed.
Example:
Controller:
The text was updated successfully, but these errors were encountered: