Skip to content

Instantly share code, notes, and snippets.

@linusthe3rd
Last active January 13, 2016 17:18
Show Gist options
  • Save linusthe3rd/ae41e2742231579ad4e7 to your computer and use it in GitHub Desktop.
Save linusthe3rd/ae41e2742231579ad4e7 to your computer and use it in GitHub Desktop.

Revisions

  1. linusthe3rd revised this gist Jan 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ var loadUser = function (id) {
    }

    var subscribeToNotifications = function(id) {
    // only subscribe to notifications if
    // only subscribe to notifications if we have a web socket open
    if (notifications.isConnected()) {

    notifications.subscribe("users."+id, onNotification)
  2. linusthe3rd revised this gist Jun 24, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ var subscribeToNotifications = function(id) {
    // only subscribe to notifications if
    if (notifications.isConnected()) {

    notifications.subscribe("users."+id)
    notifications.subscribe("users."+id, onNotification)
    .done(function () {
    loadUser(id);
    });
  3. linusthe3rd created this gist Jun 17, 2014.
    46 changes: 46 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    // ...other instantiation logic...

    var userID = getIDFromPageURI();
    var user = null;

    var loadUser = function (id) {
    // GET the latest state of the user from the backend
    $.get("/api/user" + id)
    .done(function (response) {
    if (!user) {
    // if the user variable is NOT defined yet, create a new instance of a UserModel.
    user = new UserModel(response);
    } else {
    // If the user variable IS defined, fill in missing data
    UserModel.fill(user, response);
    }
    });
    }

    var subscribeToNotifications = function(id) {
    // only subscribe to notifications if
    if (notifications.isConnected()) {

    notifications.subscribe("users."+id)
    .done(function () {
    loadUser(id);
    });
    }
    }

    var onNotification = function (data) {
    if (!user) {
    // If the user variable is NOT defined, create a new instance of the user model.
    user = new UserModel(data);
    } else {
    // If the user variable IS defined, merge the new data into the existing model
    UserModel.merge(user, data);
    }

    }

    // Now that the page is defined, load the data from the backend and subscribe to notifications
    loadUser(userID);
    subscribeToNotifications(userID);

    // ...other instantiation logic...