lundi 27 février 2017

How to make module pattern each function a promise?

I use Angular 1.5 and I made a factory function which is return a literal object like this:

return {
   item: null,
   get: function() {
     return item;
   },
   create: function() {
     if (this.get()){
         this.remove();
     }

     this.item = {};
   },
   remove: function() {
     var item = this.get();
     if (item) {
      this.item = null;
     }
   },
   add: function() {
     if (!this.get()) {
        this.create();
     }

     this.item.newprop = 'value';
   }
}

  1. please do not ask me to change to function declaration. I want a object with his own actions(functions) and properties that is working on.

  2. This pattern (like get inside create so on..) I didn't copied from anywhere. so I'm wonder if has a name? It is best way to deal with function-black boxes?

  3. What is the best way to put Promise inside? so every function should return a promise

  4. every then function I need to use bind???

    todo like this:

create: function () {
    this.get()
        .then(remove)
        .then(function () {
            this.item = {}; // BUT this === undefined!!
        });
}

Aucun commentaire:

Enregistrer un commentaire