GithubHelp home page GithubHelp logo

gradconnection / immstruct-construct Goto Github PK

View Code? Open in Web Editor NEW
0.0 9.0 0.0 140 KB

Initialize localized immstruct data within each component, rather than forcing the building of the immstruct at the top.

JavaScript 100.00%

immstruct-construct's Introduction

This module provides the ability to initialize localized immstruct data within each component, rather than forcing the building of the immstruct at the top.

Usage

    /* This module provides the ability to initialize localized immstruct data
    within each component, rather than forcing the building of the immstruct at the
    top. */

    var immstruct = require("immstruct")
    var createConstruct = require('./index').createConstruct;

    /* Imagine attaching this function to FriendListItem.construct,
       FriendListItem is a `component`. */
    var FriendListItemState = createConstruct(function(name) {
      return {
        firstName: name,
        country: new Promise(function(resolve, reject) {
          setTimeout(function() {
            // Does a 1 second lookup, simulating API access.
            resolve(this.state.routeParams.country)
          }.bind(this), 1000)
        }.bind(this)),
      }
    })

    /* Imagine attaching this function to FriendList.construct,
       FriendList is a `component`.*/
    var FriendListState = createConstruct(function(names) {
      return {
        friendListItemStates: Promise.all(names.map(
            function(name, i) {
              return FriendListItemState(this, ["friendListItemStates", i], name)
            }.bind(this)))
      };
    })

    /* Imagine attaching this function to App.construct,
       App is a `component`.*/
    var AppState = createConstruct(function() {
      return {
        friendListState: FriendListState(this, "friendListState", 
          ["John", "Shepard", "Liara"])
      }
    });

    var structure = immstruct({});

    structure.on('swap', function(a, b, path) {
      console.log("swap at path:", path);
    })

    AppState({
      structure: structure,
      path: [],
      state: {
        routeParams: { country: "Australia" }
      }
    }).then(function() {
      console.log("All promises complete. structure:")
      console.log(JSON.stringify(structure.cursor().deref(), null, " "));
    });

    /* output:

    swap at path: [ 'friendListState', 'friendListItemStates', 0 ]
    swap at path: [ 'friendListState', 'friendListItemStates', 1 ]
    swap at path: [ 'friendListState', 'friendListItemStates', 2 ]
    swap at path: [ 'friendListState', 'friendListItemStates', 0 ]
    swap at path: [ 'friendListState', 'friendListItemStates', 1 ]
    swap at path: [ 'friendListState', 'friendListItemStates', 2 ]
    swap at path: [ 'friendListState' ]
    All promises complete. structure:
    {
     "friendListState": {
      "friendListItemStates": [
       {
        "firstName": "John",
        "country": "Australia"
       },
       {
        "firstName": "Shepard",
        "country": "Australia"
       },
       {
        "firstName": "Liara",
        "country": "Australia"
       }
      ]
     }
    }
    */

To run example:

 npm install
 node usage.js

immstruct-construct's People

Contributors

meric avatar

Watchers

 avatar James Cloos avatar Jonathan Pentecost avatar Den Angeles avatar  avatar  avatar  avatar Michael Whelehan avatar Rita Biankin avatar

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.