1

I am trying to structurise JS code using Revealing Prototype Pattern.

My basic usecase is: Create different types of entities like Person, Technology. Each entity has its own tags. In order to get these tags i make an ajax call which returns an object of tags. I want to access this object in my implementation. But i am not sure how to do it in right way.

My attempt is as follows:

var Entity= function (url) {
    this.url = url; /*variable that can be shared by all instances*/
    var entityTags; 
};

Entity.prototype = function () {
    var create = function (type, values) {
    //code for creating
}

var update = function (type, values) {}

var tags = function () {
    AjaxCall(' ', this.url, {data:data}, 'callbackAfterGetTags', '');
    callbackAfterGetTags=function(responseFromAjax)
    {
        entityTags=responseFromAjax.tagsReturned; //how to access this entityTags in my implementation
    }
};
return {
     createEntity: create,
     getTagsEntity: tags
};

My Implementation

var myEntity = new Entity(url);
myEntity.getTagsEntity();

Ajax call returns object successfully but i am not sure how to access the object inside tags functions in a right way. Any suggestions? This is my first trial to use OO style in JS. Let me also know if i am right track or not.

jrbedard
  • 3,662
  • 5
  • 30
  • 34
user596502
  • 417
  • 3
  • 10
  • 22
  • Uses promises. Have a look at [How do I return the response from an asynchronous call?](http://stackoverflow.com/q/14220321/1048572) and [Is it bad practice to have a constructor function return a Promise?](http://stackoverflow.com/q/24398699/1048572) – Bergi Sep 22 '16 at 16:17

0 Answers0