Utilizing Angular 1.5, I have developed a factory function that returns a literal object structured as follows:
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';
}
}
I prefer to keep the object structure with its own actions and properties rather than changing to function declarations.
The pattern of nesting functions like
get
insidecreate
is original to my design. Does this approach have a specific name and is it considered an optimal way to handle function-black boxes?What would be the most effective method to incorporate Promises within each function so that they all return a Promise?
Is it necessary to use
bind
for everythen
function call?For example:
create: function () { this.get() .then(remove) .then(function () { this.item = {}; // However, in this context, 'this' is undefined!! }); }