Skip to content
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

Reactive arrays #44

Closed
sachalifs opened this issue Oct 21, 2014 · 3 comments
Closed

Reactive arrays #44

sachalifs opened this issue Oct 21, 2014 · 3 comments

Comments

@sachalifs
Copy link

I have a property set to an array and I want to be able to get its length attribute on the template.
Is that possible using ripplejs/ripple standalone or using any plugin?

My code looks like the following:

var ripple = require('ripple');
var template = '<div>{{items.length}}</div>';
var View = ripple(template);

View.attr('items', {type: 'array'});

View.on('ready', function(view) {
  setTimeout(function () {
    view.get('items').push(1); // does not update the template
  }, 1000);
});

var view = new View({items: [10, 20, 30, 0, 4]});

view.appendTo(document.body);
@chrisbuttery
Copy link
Contributor

I was able to get it working by tweeking a few things

var ripple = require('ripple');
var template = '<div>{{items.length}}</div>';
var View = ripple(template);

View.on('created', function(view){
  setTimeout(function () {
    view.data.items.push(50); // add a new val
    view.set('items', view.data.items); // set the new data
  }, 1000);
});

var view = new View({
  data: {
    items: [10, 20, 30, 0, 4]
  }
});

view.appendTo('body');

@sachalifs
Copy link
Author

@chrisbuttery but you have to view.set() the items every time you update it, that was the weird thing I noticed. Isn't there any other way to work around this with arrays?

@anthonyshort
Copy link
Contributor

I intentionally left out doing any watching with arrays, just because it comes with so many weird cases it adds a lot of scope creep and bloat. Like, should we be able to watch items in the array? What if items in the array are moved around?

For simple values like in the example, setting it will work fine. But when it's an array of views it's trickier. I was working on having this functionality in a plugin: https://github.com/ripplejs/list but I didn't get around to finishing it.

I'm happy to take on any suggestions to make iteration better though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants