Giter Club home page Giter Club logo

statesman's Introduction

statesman's People

Contributors

rich-harris avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

statesman's Issues

This is cool!! Feature request!

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
:-)

state.unobserve(keypath)

Hi , thanks for the library.

How about state.unobserve(keypath), where keypath is a string. it should remove listeners for any keypath that matches.

for instance :

s.observe("video[0].title",aFunction);
s.observe("video[0].title",anotherFunction);
// 
s.unobserve("videos[0].title");
// remove the 2 previous listeners.

When observing an array of objects the returned old value is the same as the new value

This library is great! Sorry to dust this off after a couple years, but I ran into one issue. Let me know if this is just the expected behavior.

When I observe an array of objects and update a value on one of those objects, the old value that is returned is actually the new value.

Observing the specific value of the object in the array works as expected.

var state = new Statesman({
   user: {
      name: 'Alice',
      friends: [ 
         {
            name: 'Bob',
            age: 20
         }
      ]
   }
});

// oldValue is the same as newValue
state.observe( 'user', function ( newValue, oldValue ) {
   console.log(oldValue); // user.friends[0].age = 21 (instead of 20)
   console.log(newValue); // user.friends[0].age = 21
}, {init: false});

// this works as expected
state.observe( 'user.friends[0].age', function ( newValue, oldValue ) {
   console.log(oldValue); // 20
   console.log(newValue); // 21
}, {init: false});

// change Bob's age
state.set( 'user.friends[0].age', 21 );

Here is a jsFiddle - type in a new value and hit submit to see it in action.

Array wrapping

This syntax is minging:

state.set( 'data[3]', { foo: 'bar' });

// or even worse
state.set( 'data[' + state.get( 'data.length' ) + ']', { foo: 'bar' });

It would be much nicer to be able to use standard array mutator methods. We should wrap those methods so that a call to push is equivalent to the above.

More declarative style of declaring computed values

Possibly a bit blue-sky, but this syntax would be neat:

state.compute( 'width', '<% x2 %> - <% x1 %>' );

// as opposed to
state.compute( 'width', {
  triggers: [ 'x1', 'x2' ],
  fn: function ( x1, x2 ) {
    return x2 - x1;
  }
});

Probably best to use different delimiters, to avoid confusion with templating languages.

At any rate, something like this would be very compact and fairly straightforward to a) identify dependencies, and b) compile to a function.

Observe multiple values (ala computed values)

This would come in handy:

state.observe( keypath1, keypath2, function ( value1, value2 ) { /*...*/ });

// or
state.observe( [ keypath1, keypath2 ], function ( value1, value2 ) { /*...*/ });

Feature question/proposal

It would be great(possibly hard to implement) to have a way of defining observer function over coupled keypaths and make it react only when all of the defined keypaths have been changed. Lets say

sm.observe(['keypath1', 'keypath2'], function () {
  // both keypaths changed
});

Caching

It would make sense to flatten data and cache it where possible, rather than doing a graph traversal each time

Events

Statesman instances (Statesmen?) should emit events when data changes. Some useful events:

  • set
  • change
  • delete/remove

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.