How to setup Ember like computed properties in Immutablejs and Redux and Flux and React

前端 未结 4 482
旧巷少年郎
旧巷少年郎 2021-01-05 01:11

I am used to computed properties in Ember Object Model. It\'s a convenient way to specify computed properties that depend on other properties.

Say fullName

4条回答
  •  情深已故
    2021-01-05 02:00

    To create computed properties you can use the standalone observable library mobservable.

    var user = mobservable.props({
      firstName: 'John',
      lastName: 'Doe',
      fullName: function() {
        return this.firstName + this.lastName
      }
    });
    
    var nameViewer = mobservable.ObservingComponent(React.createClass({
       render: function() {
           return ({user.fullName})
       }
    });
    

    That should be the gist of it, now any change to user.firstName or lastName will rerender your nameViewer component. You can further combine this with flux implementations like redux to change the data, and push the user itself through your component tree. But note that the user object itself is not immutable (in that case it wouldn't be observable after all ;-)) Also see this trivial and slightly more interesting fiddles for some examples.

提交回复
热议问题