lundi 22 mai 2017

How can I improve this Javascript Pattern Code [on hold]

I have created a component factory/ish function that allows for easy reuse of components. Can anyone advise me if I have done anything wrong (Bad Practice/Performance) and if I can improve this further? See JSFiddle for full example.

http://ift.tt/2rIyhHp

// Register each component
$("div[data-component]").each(function() {
     NJJ.registerComponent($(this));
});

NJJ.registerComponent = function($component) {
  var componentType = "",
    dataAttr = $.each($component.data(), function(key, value) {
      key: value
    });
  componentType = NJJ.componentMapping[dataAttr.component] || false;
  if (componentType) {
    try {
      return NJJ.activeComponents.push(new componentType($component, dataAttr))
    } catch (e) {
      console.warn(e.message);
      var err = {
        "component": dataAttr.component,
        "action": "Registered Component Error, " + e.message
      };
      NJJ.componentAnalytics(err);
    }
  } else {
    console.warn("Warning, Unregistered component: " + dataAttr.component);
    var err = {
      "component": dataAttr.component,
      "action": "Failed to initiate, Unregistered component"
    };
    NJJ.componentAnalytics(err);
  }
};

NJJ.componentLibrary.Slider = function($component, dataAttr) {
  // Individual Component functions in here
  console.log("Slider Component Registered");
  console.log(dataAttr);

  $component.html("Unique Test Slider Component: ID-" + Math.random());
};

Aucun commentaire:

Enregistrer un commentaire