Giter Club home page Giter Club logo

component.js's People

Contributors

gravityblast avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

component.js's Issues

Modifications for Turbolinks

Finally managed to play around with it and it works really good. I thought about some modifications that can make life better when using it with Turbolinks and I'd like to hear your ideas.

Let's say you do some polling on the confirmation page that will tell the user if their booking has been accepted by the seller.

setTimeout(checkIfSellerResponded, 2000);

With Turbolinks on, visiting another page will still keep making that request. So it would be nice if along with the execution of the component, you could unmount it as well.

The components will need to change the way of registering a component. Instead of accepting the name of the component and a func, it will accept and object with the name, an onMount and an unMount function.

// component.js
(function (Components) {
  Components.register({name: 'the-name', onMount: sellerResponded, unMount: removePolling});
  ...
})(Components);
// components.js

Components = {
  ...
  register: function(component) {
    this.components[component.name] = component;
  },
  ...
}

Now that we have a way to unmount a component, each time we Components.init we can unmount them. What is left to do is to keep track of what components we have initialized on each page:change.

// components.js

Components = {
  components: {},
  initializedComponents: {}, // Keeps track of initializedComponents per page

  init: function () {
    var elements = $('[data-js-component]');
    // On each init, we unmount the initialized components
    this.unMountInitializedComponents(this.initializedComponents);
    ...
  },

  ...

  initializeComponent: function ($element, component) {
    ....
    component.onMount($element, options);
    // Adding it to the initialized collection
    this.initializedComponents[component.name] = component
  },

  unMountInitializedComponents: function (components) {
    for (component in components) {
     // a component does not need to define an unMount method. For example a Tabs component, doesn't need to be unmounted.
      if (components[component].unMount) {
        components[component].unMount();
      }
    }

    this.initializedComponents = {}; // We are resetting the initializedComponents
  }
}

WDYT?

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.