Search code examples
javascriptmethodsjavascript-objectsprototype-programming

Javascript Object communication and get parameter from other object


I have a problem with share parameters from one object to other I have one LatestVideos object with options my video galleries and other object with methods to Paginate, Render, Categories and LocalStorage, witch handup all functionality but I use this to many this I need this as separete object

    (function (window, document, none) {
  "use strict";
  var LatestVideos = window.LatestVideos = function (option) {
   /* object to init puglin data JSON FORMAT keys Jsondata or url , container, 
   actual page wrapper on paggination, number of item per page, categorywrapper*/
    this.fragments = document.createDocumentFragment();
    this.categories = ["Favorite"];
    this.statusLoad = 0;
    this.allid = [];
    this.categoryid = [];
    this.actual = [];
    //this.categoryChecked=[];
    this.page = 0;
    loadJSON(option.url,this.initData.bind(this));     
    this.settings = { /// init data from options object from parameter constructor      
        JsonData :option.data || 0,       
        container : option.container,
        actpage : option.actpage || 1, 
        buttonwrapper : option.paginationwrapper,
        categorywrapper : option.categorywrapper,
        itemperpage : option.itemperpage,  // get value from prev Selection or default        
    };      
  }; 
  LatestVideos.prototype.initData = function (data) { // assinchrounous call json with ajax
      this.settings.JsonData = data;
      this.settings.lengthData = data.length;
      Render.setData(this);
      Render.getCategories();
  };
  var Render = { // need this data from LASTESTVIDEOS    data,conteiner,paginationwrapper,categorywrapper
    /// object with method  to render articles to my website
  };
  var Pagination = function(){
    //   from LASTESTVIDEOS I need JsonData, actPage overide page and paginatorwrapper
    // object with method to calculate number of pages and paginate my articles
  };
  var Cateogry = function(){
     //   from LASTESTVIDEOS categoryid change actual and allid 
    // object with method to changeCategory and get category from data atribut 
   };
  var LocalStoraget = function (){
    // I JSON data from LASTESTVIDEOS
    // object with method to getFavorite item form localstorage and add to localstorage
};
})(window, document);


function loadJSON(url, callback) {  
  /* function to load ajax from url input(url- form and callback function),
 output function call and post (ARRAY JSON OBJECTS)*/
  var xmlhttp =0;
  if (typeof XMLHttpRequest !== 'undefined') {
    xmlhttp = new XMLHttpRequest();
  } else {
    var versions = ["Microsoft.XmlHttp",
        "MSXML2.XmlHttp",
        "MSXML2.XmlHttp.3.0",
        "MSXML2.XmlHttp.4.0",
        "MSXML2.XmlHttp.5.0"];
    var len =  versions.length;
    for (var i = 0; i < len; i++) {
      try {
        xmlhttp = new ActiveXObject(versions[i]);
        break;
      }
      catch(e){}
  } 
}
xmlhttp.onreadystatechange = ensureReadiness;
function ensureReadiness(){
  if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
  {
    JSONObject = JSON.parse(xmlhttp.responseText);
    callback (JSONObject);
  }else{
    return;
  }
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
var options = {url:"someURL",container:"videox",paginationwrapper:"pages"};
var opp = new LatestVideos(options);

and this is a plugin witch I need create xtimes with differend options and sometimes I need separetly render or paginate object or localstorage


Solution

  • I would probably change it a bit:

    var Render = function (options) { 
      this.options = $.extend({ //default options
        data: [],
        container: null
      }, options);
    
      // use internal logic here
      var getCategories = function(){
        ...
      };
      // and return public methods
    
      return {
        getCategories: getCategories
      };
    };
    

    and change the code in LatestVideos to the following:

    LatestVideos.prototype.initData = function (data) { // assinchrounous call json with ajax
          this.settings.JsonData = data;
          this.settings.lengthData = data.length;
          var renderer = new Render({
              data: this.settings.JsonData,
              container: this.settings.container
          });
          renderer.getCategories();
      };