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

Model Events #7

Merged
merged 4 commits into from
Jan 13, 2015
Merged

Model Events #7

merged 4 commits into from
Jan 13, 2015

Conversation

refractalize
Copy link
Member

How can the model update the view when it changes?

The model can be an event emitter and the view can subscribe to those events, refreshing when they fire.

E.g.

var $ = require('jquery');
var plastiq = require('.');
var h = plastiq.html;
var bind = plastiq.bind;

function render(model) {
  return h('div',
      h('div',
        'promise ',
        h.promise(model.promise, {
          pending: function () {
            return 'pending';
          },
          fulfilled: function (value) {
            return 'fulfilled: ' + value;
          },
          rejected: function (reason) {
            return 'rejected: ' + reason;
          }
        })
      ),
      h('div',
        'state: ',
        h.promise(model.state, {
          pending: 'pending',
          fulfilled: 'fulfilled: ',
          rejected: 'rejected: ',
        })
      ),
      h.animation(model.animation.bind(model)),
      h('div', 'animated text: ', model.text)
    );
}

var model = {
  promise: new Promise(function (result, error) {
    setTimeout(function () {
      result('haha');
    }, 1000);
  }),
  state: $.get('/state'),
  animation: function (render) {
    var self = this;
    self.text = 'start';
    render();
    setTimeout(function () {
      self.text = 'middle';
      render();
      setTimeout(function () {
        self.text = 'end';
        render();
      }, 1000);
    }, 1000);
  }
};

plastiq.attach(document.body, render, model);

@refractalize
Copy link
Member Author

This is a different solution to #3 and #4.

@refractalize refractalize merged commit 55f85fa into master Jan 13, 2015
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

Successfully merging this pull request may close these issues.

None yet

1 participant