Giter VIP home page Giter VIP logo

Comments (3)

anthonyshort avatar anthonyshort commented on July 4, 2024

However, this could break if you happened to drop in another component that listened for the same event. Maybe it would be best to just use actions and stores for this.

from deku.

yoshuawuyts avatar yoshuawuyts commented on July 4, 2024

I'm +1 on using stores/actions to communicate state. With React passing this to child components has worked pretty well for tiny parts, where stores/actions are good at orchestrating the larger moving parts.

from deku.

anthonyshort avatar anthonyshort commented on July 4, 2024

We ended up going with a concept called channels. This allows a flux-like interface for components. I'll be adding some docs for this soon.

var List = component()
  .channel('todos');

List.prototype.beforeMount = function(props, state) {
   var self = this;
   props.channels.todos.on('change', function(todos){
     self.setState('todos', todos);
   });
}; 

List.prototype.render = function(props, state) {
   return dom('ul', state.todos.map(function(todo){
      return dom('li', todo.name);
   }));
}; 

var scene = List.render(document.body);

// Create the channel
var channel = scene.channel('todos');

// Whenever a component connects to this channel we
// can send data to it. These will automatically get cleaned up.
channel.on('connection', function(socket){
  api.todos.on('change', function(todos){
    socket.emit('change', todos);
  });
});

The components are still dumb and don't care where they get data from. The socket that gets passed into props.channels.todos is just an event emitter. So it's easy to fake in tests. I'm still seeing if this is a good solution but it's pretty decoupled.

from deku.

Related Issues (20)

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.