Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

This is cool!! Feature request! #1

Closed
1N50MN14 opened this issue Mar 20, 2013 · 2 comments
Closed

This is cool!! Feature request! #1

1N50MN14 opened this issue Mar 20, 2013 · 2 comments

Comments

@1N50MN14
Copy link

Rich!! You got some goodies in here!! Statesman is awesome as hell!, gonna use this one instead! :-)

Feature request! (which plays well with Anglebars)

It would be GREAT if something like the following was possible:

var observers = state.observe( 'todos.*.completed', function ( newValue, oldValue ) {
   var total = state.get('total_completed');
   total += newValue ? +1 : -1;
   state.set('total_completed', total);
   tpl.update();
});

(the above can obviously be achieved as well by setting an observer on every single item)

Check this out: https://github.com/deoxxa/dotty/blob/master/lib/index.js
:-)

@Rich-Harris
Copy link
Owner

Thanks man! I've got you covered! You need to use computed values:

state = new Statesman({ todos: todos });

state.compute( 'completed_todos', {
  trigger: 'todos',
  fn: function ( todos ) {
    return todos.filter( function ( t ) { return t.completed; })
  }
});

state.observe( 'completed_todos.length', function ( num ) {
  alert( num + ' todos completed' );
}, true ); // passing in true here means it fires straight away rather than waiting for a change

I thought about doing some kind of wildcard/regex-based observers, but eventually decided against it because it would mean that whenever any part of the model changed, all the wildcard patterns would need to be tested (as opposed to just doing property lookups, as at present) which could get slow with a complex model. All of the things I could think of wanting to do with wildcards can be done with computed values, and I believe it's more efficient that way.

I hadn't seen dotty, will check it out, cheers.

Yeah, this is a better tool for overall state management than Anglebars - an Anglebars instance is only really concerned with its own state, whereas this is designed to be central to an app. Of course, you can bind the two together with e.g.

state.observe( 'todos', function ( todos ) {
  anglebars.set( 'todos', todos );
}, true );

(I might come up with a neater syntax for combining the two but don't really know if it's worth it.)

@1N50MN14
Copy link
Author

Ah you make valid points! and yeah, not worth the effort combining the two.

PS: I good some goodies coming your way over the weekend :-)

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

No branches or pull requests

2 participants