Skip to content

Instantly share code, notes, and snippets.

@silverbucket
Forked from jdanyow/app.html
Last active June 29, 2016 12:56
Show Gist options
  • Select an option

  • Save silverbucket/9538acd77d0403da3c691fad6149c63a to your computer and use it in GitHub Desktop.

Select an option

Save silverbucket/9538acd77d0403da3c691fad6149c63a to your computer and use it in GitHub Desktop.

Revisions

  1. silverbucket revised this gist Jun 29, 2016. No changes.
  2. silverbucket revised this gist Jun 29, 2016. 3 changed files with 162 additions and 5 deletions.
    8 changes: 7 additions & 1 deletion app.html
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    <template>
    <h1>${message}</h1>
    <h1>Files List</h1>
    <button click.trigger="addFile()">Add File</button>
    <ul>
    <li repeat.for="file of files._store">
    <p>${file.uid} - ${file.name}</p>
    </li>
    </ul>
    </template>
    158 changes: 155 additions & 3 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,160 @@
    export class App {
    message = 'Hello World!';
    test = new Test();
    files = new ArrayKeys();

    constructor() {
    this.files = new ArrayKeys({
    identifier: 'uid'
    });

    }

    addFile() {
    var uid = makeid(5);
    console.log('adding file: ' + uid);
    var file = {
    uid: uid,
    name: 'filename-' + uid
    };
    this.files.addRecord(file);
    console.log('files: ', this.files._store);
    }
    }

    function Test() {}
    function makeid(count) {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    for (var i = 0; i < count; i++) {
    text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
    return text;
    }

    function ArrayKeys(p) {
    if (typeof p !== 'object') { p = {}; }
    this._identifier = p.identifier || 'id';
    this._store = [];
    this._idx = []; // array of identifier strings for quick lookup
    this._emitEvents = false;
    }

    ArrayKeys.prototype.emitEvent = function (event, data, dontEmit) {
    if ((this._emitEvents) && (! dontEmit)) {
    this.events.emit(event, data);
    }
    };

    ArrayKeys.prototype.getIdentifiers = function () {
    var ids = [];
    for (var i = this._store.length - 1; i >= 0; i = i - 1) {
    ids[ids.length] = this._store[i][this._identifier];
    }
    return ids;
    };

    ArrayKeys.prototype.getRecord = function (id) {
    for (var i = this._store.length - 1; i >= 0; i = i - 1) {
    if (this._store[i][this._identifier] === id) {
    return this._store[i];
    }
    }
    return undefined;
    };

    ArrayKeys.prototype.exists = function (id) {
    if (this.getIndex(id) >= 0) {
    return true;
    } else {
    return false;
    }
    };

    // faster than using indexOf
    ArrayKeys.prototype.getIndex = function (id) {
    for (var i = this._idx.length - 1; i >= 0; i = i - 1) {
    if (this._idx[i] === id) {
    return i;
    }
    }
    return -1;
    };

    ArrayKeys.prototype.addRecord = function (record) {
    if (typeof record !== 'object') {
    throw new Error('cannot add non-object records.');
    } else if (!record[this._identifier]) {
    throw new Error('cannot add a record with no `' + this._identifier +
    '` property specified.');
    }

    var removed = this.removeRecord(record[this._identifier], true);
    this._idx[this._idx.length] = record[this._identifier];
    this._store[this._store.length] = record;
    setTimeout(function () {
    if (removed) {
    setTimeout(this.emitEvent.bind(this, 'update', record), 0);
    } else {
    setTimeout(this.emitEvent.bind(this, 'add', record), 0);
    }
    }.bind(this), 0);
    return true;
    };

    ArrayKeys.prototype.removeRecord = function (id, dontEmit) {
    var idx = this.getIndex(id);
    if (idx < 0) {
    return false;
    }

    // start looking for the record at the same point as the idx entry
    for (var i = idx; i >= 0; i = i - 1) {
    if ((this._store[i]) && (this._store[i][this._identifier] === id)) {
    this._store.splice(i, 1);
    this._idx.splice(idx, 1);
    setTimeout(this.emitEvent.bind(this, 'remove', id, dontEmit), 0);
    return true;
    }
    }

    // if it was not found, start at the end and break at the idx number
    for (var n = this._store.length - 1; n >= idx; n = n - 1) {
    if ((this._store[i]) && (this._store[n][this._identifier] === id)) {
    this._store.splice(n, 1);
    this._idx.splice(idx, 1);
    setTimeout(this.emitEvent.bind(this, 'remove', id, dontEmit), 0);
    return true;
    }
    }
    return false;
    };

    ArrayKeys.prototype.forEachRecord = function (cb) {
    var count = 0;
    var self = this;
    var finished = function () {};

    setTimeout(function () {
    for (var i = self._store.length - 1; i >= 0; i = i - 1) {
    count += 1;
    setTimeout(cb(self._store[i]), 0);
    }
    setTimeout(finished(count), 0);
    }, 0);

    return {
    finally: function (func) {
    finished = func;
    }
    };
    };

    ArrayKeys.prototype.getCount = function () {
    return this._store.length;
    };

    ArrayKeys.prototype.removeAll = function () {
    for (var i = this._store.length - 1; i >= 0; i = i - 1) {
    delete this._store[i];
    }
    this._store = [];
    };

    1 change: 0 additions & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,6 @@
    <body aurelia-app>
    <h1>Loading...</h1>

    <script src="https://raw.githubusercontent.com/silverbucket/array-keys/master/browser/array-keys.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
  3. silverbucket revised this gist Jun 29, 2016. 2 changed files with 6 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    export class App {
    message = 'Hello World!';
    test = new Test();
    files = new ArrayKeys();
    }

    function Test() {}

    1 change: 1 addition & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@
    <body aurelia-app>
    <h1>Loading...</h1>

    <script src="https://raw.githubusercontent.com/silverbucket/array-keys/master/browser/array-keys.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
  4. @jdanyow jdanyow revised this gist Apr 12, 2016. No changes.
  5. @jdanyow jdanyow revised this gist Apr 11, 2016. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,12 @@
    <body aurelia-app>
    <h1>Loading...</h1>

    <script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/jspm_packages/system.js"></script>
    <script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/config.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
    <script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
    <script>
    System.import('aurelia-bootstrapper');
    require(['aurelia-bootstrapper']);
    </script>
    </body>
    </html>
  6. @jdanyow jdanyow revised this gist Mar 6, 2016. 3 changed files with 3 additions and 23 deletions.
    7 changes: 3 additions & 4 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,13 @@
    <html>
    <head>
    <title>Aurelia</title>
    <link rel="stylesheet" href="styles.css">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body aurelia-app="main">
    <body aurelia-app>
    <h1>Loading...</h1>

    <script src="https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.11.10/jspm_packages/system.js"></script>
    <script src="https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.11.10/config2.js"></script>
    <script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/jspm_packages/system.js"></script>
    <script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/config.js"></script>
    <script>
    System.import('aurelia-bootstrapper');
    </script>
    17 changes: 0 additions & 17 deletions main.js
    Original file line number Diff line number Diff line change
    @@ -1,17 +0,0 @@
    /*******************************************************************************
    * The following two lines enable async/await without using babel's
    * "runtime" transformer. Uncomment the lines if you intend to use async/await.
    *
    * More info here: https://github.com/jdanyow/aurelia-plunker/issues/2
    */
    //import regeneratorRuntime from 'babel-runtime/regenerator';
    //window.regeneratorRuntime = regeneratorRuntime;
    /******************************************************************************/

    export function configure(aurelia) {
    aurelia.use
    .standardConfiguration()
    .developmentLogging();

    aurelia.start().then(a => a.setRoot());
    }
    2 changes: 0 additions & 2 deletions styles.css
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    /* Styles go here */

  7. @jdanyow jdanyow created this gist Feb 22, 2016.
    3 changes: 3 additions & 0 deletions app.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    <template>
    <h1>${message}</h1>
    </template>
    3 changes: 3 additions & 0 deletions app.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    export class App {
    message = 'Hello World!';
    }
    17 changes: 17 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    <!doctype html>
    <html>
    <head>
    <title>Aurelia</title>
    <link rel="stylesheet" href="styles.css">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body aurelia-app="main">
    <h1>Loading...</h1>

    <script src="https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.11.10/jspm_packages/system.js"></script>
    <script src="https://cdn.rawgit.com/jdanyow/aurelia-plunker/v0.11.10/config2.js"></script>
    <script>
    System.import('aurelia-bootstrapper');
    </script>
    </body>
    </html>
    17 changes: 17 additions & 0 deletions main.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    /*******************************************************************************
    * The following two lines enable async/await without using babel's
    * "runtime" transformer. Uncomment the lines if you intend to use async/await.
    *
    * More info here: https://github.com/jdanyow/aurelia-plunker/issues/2
    */
    //import regeneratorRuntime from 'babel-runtime/regenerator';
    //window.regeneratorRuntime = regeneratorRuntime;
    /******************************************************************************/

    export function configure(aurelia) {
    aurelia.use
    .standardConfiguration()
    .developmentLogging();

    aurelia.start().then(a => a.setRoot());
    }
    2 changes: 2 additions & 0 deletions styles.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    /* Styles go here */