Skip to content

Instantly share code, notes, and snippets.

@mmirolim
Last active August 29, 2015 14:03
Show Gist options
  • Save mmirolim/671b8a53a1115d65c83b to your computer and use it in GitHub Desktop.
Save mmirolim/671b8a53a1115d65c83b to your computer and use it in GitHub Desktop.
Modifications 06.07.14
// DEFINE cache _ID in one place
// like for update, create, last disconnect and so on
// add children collection to task model
children:{
collection: 'task',
via: 'parentTaskId'
// fix problem with sails crash (waterline) when model send with extra fields
// clean obj before so sails will not crash, should be cleaned in TaskController actions update and create
_(newTask).forEach( function (v, k) {
if (!Task.attributes.hasOwnProperty(k)) {
delete task[k];
}
});
delete newTask.children;
// populate children in task
getTasks : function (groupId, criteria, next) {
this.findOne(groupId)
.then(function(group){
// populate task with children tasks
Task.find({where: {grpId: groupId}})
.populate('children')
.then(function (tasks) {
/* @TODO find why cant reach group object in this scope ?
could not assign tasks with children to tasks property of group
console.log(group.tasks, 'Children');
http://stackoverflow.com/questions/24571742/how-to-reference-associated-models-in-a-one-to-many-relationship-with-sails-0-10
*/
var groupTasks = {
id: group.id,
createdAt: group.createdAt,
updatedAt: group.updatedAt,
title: group.title,
info: group.info,
tasks: tasks
};
next(groupTasks, undefined);
})
.fail(function(err) {
console.log(err);
})
})
.fail(function(err){
next(undefined, err);
});
},
// show connected status
// select ui element to show status of connection
var onlineStatus = document.getElementsByClassName('offline-ui')[0];
// logic to handle disconnects
io.socket.on('disconnect', function () {
console.log('App is Offline');
onlineStatus.setAttribute('class', 'offline-ui offline-ui-down');
});
// listen to socket connect and sync pending data (tasks)
io.socket.on('connect', function () {
// set online status
onlineStatus.setAttribute('class', 'offline-ui offline-ui-up');
console.log('App is Online');
// html template mode for children task
<ul>
<li ng-repeat="child in task.children" class="{{ child.status }}">
<a href="#" title="{{ child.title }}"></a>
</li>
</ul>
// show task in scope with index and ctrl name
$scope.showObj = function (index) {
if (window.event.ctrlKey) {
console.log($scope.tasks[index], 'Index in scope: ' + index + '; Controller: ' + $scope.ctrlName);
}
};
<!-- offline indicator -->
<div data-phase="2" class="offline-ui offline-ui-down">
<div class="offline-ui-content" data-retry-in="5 seconds" data-retry-in-abbr="5s"></div>
<a class="offline-ui-retry"></a>
</div>
// css styles for offline indicator
.offline-ui-up .offline-ui-content:before {
content: 'Online';
}
.offline-ui-down .offline-ui-content:before {
content: 'Offline';
}
/* line 3, ../sass/_offline-theme-base-indicator.sass */
.offline-ui, .offline-ui *, .offline-ui:before, .offline-ui:after, .offline-ui *:before, .offline-ui *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
/* line 6, ../sass/_offline-theme-base-indicator.sass */
.offline-ui {
display: none;
position: fixed;
background: white;
z-index: 2000;
display: inline-block;
}
/* line 13, ../sass/_offline-theme-base-indicator.sass */
.offline-ui .offline-ui-retry {
display: none;
}
/* line 16, ../sass/_offline-theme-base-indicator.sass */
.offline-ui.offline-ui-up {
display: block;
}
/* line 19, ../sass/_offline-theme-base-indicator.sass */
.offline-ui.offline-ui-down {
display: block;
}
/* line 8, ../sass/offline-theme-chrome-indicator.sass */
.offline-ui {
-webkit-box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15);
-moz-box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15);
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15);
-webkit-border-radius: 4px 4px 0 0;
-moz-border-radius: 4px 4px 0 0;
-ms-border-radius: 4px 4px 0 0;
-o-border-radius: 4px 4px 0 0;
border-radius: 4px 4px 0 0;
font-family: "Lucida Grande", sans-serif;
font-size: 12px;
padding: 7px;
width: 69px;
background: #f6f6f6;
color: #888888;
bottom: 0;
left: 20px;
}
/* line 20, ../sass/offline-theme-chrome-indicator.sass */
.offline-ui .offline-ui-content {
padding-left: 16px;
}
/* line 23, ../sass/offline-theme-chrome-indicator.sass */
.offline-ui .offline-ui-content:after {
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
-ms-border-radius: 50%;
-o-border-radius: 50%;
border-radius: 50%;
content: " ";
display: block;
position: absolute;
top: 0;
bottom: 1px;
left: 8px;
margin: auto;
height: 9px;
width: 9px;
}
/* line 37, ../sass/offline-theme-chrome-indicator.sass */
.offline-ui.offline-ui-up .offline-ui-content:after {
background: #80d580;
}
/* line 42, ../sass/offline-theme-chrome-indicator.sass */
.offline-ui.offline-ui-down .offline-ui-content:after {
background: #ec8787;
}
// save disconnect timestamp
// remove last disconnect timestamp on connect
var _id = "last/disconnect/timestamp";
db.get(_id, function (err, doc) {
if (!err) {
db.remove(doc, function (err, r) {
if (!err) {
console.log(r, 'Last disconnect timestamp removed');
} else {
console.log(err);
}
})
} else {
console.log(err);
}
});
// set timestamp when disconnected to be able to chech what we missed
var _id = "last/disconnect/timestamp";
var timestamp = {
time: Date.now()
};
db.get(_id, function (err, doc) {
if (!err) {
db.put(timestamp, _id, doc._rev, function (err, doc) {
if (!err) {
devPanel.log(doc);
} else {
console.log(err);
}
});
} else {
db.put(timestamp, _id, function (err, doc) {
if (!err) {
console.log(doc, "Cached");
} else {
console.log(err);
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment