Created
September 26, 2013 19:17
-
-
Save asakasinsky/6719174 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ArrayModel = function(){}; | |
ArrayModel.prototype = []; | |
_.extend(ArrayModel.prototype, { | |
pointer: -1, | |
next: function () | |
{ | |
if (!((this.pointer + 1) in this)) {return false;} | |
this.pointer = this.pointer + 1; | |
return this; | |
}, | |
prev: function () | |
{ | |
if (!((this.pointer - 1) in this)) {return false;} | |
this.pointer = this.pointer - 1; | |
return this; | |
}, | |
first: function () | |
{ | |
this.pointer = 0; | |
return this; | |
}, | |
last: function () | |
{ | |
this.pointer = this.length - 1; | |
return this; | |
}, | |
remove: function () | |
{ | |
return this.splice(this.pointer, 1)[0]; | |
}, | |
replace: function (arr) | |
{ | |
return this.splice(this.pointer, 1, arr)[0]; | |
}, | |
insertAfter: function (arr) | |
{ | |
this.pointer = this.pointer + 1; | |
return this.splice(this.pointer, 0, arr); | |
}, | |
swap: function (index_a, index_b) | |
{ | |
if (this.length === 1) {return this;} | |
this.splice(index_b, 1, this.splice(index_a, 1, this[index_b])[0]); | |
return this; | |
}, | |
set_pointer: function (index) | |
{ | |
index = parseInt(index, 10) || 0; | |
this.pointer = index; | |
return this; | |
}, | |
get: function () | |
{ | |
if ( typeof this[this.pointer] === 'function' ) { | |
return this[this.pointer](); | |
} | |
return this[this.pointer]; | |
} | |
}); | |
var screenplay = new ArrayModel(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment