Skip to content

Commit

Permalink
mv getAccounts to app-feed and encapsulate viewmodel: changeAccout,ch…
Browse files Browse the repository at this point in the history
…angeScope
  • Loading branch information
daneroo committed May 28, 2012
1 parent d785eeb commit d93f5e5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 40 deletions.
4 changes: 2 additions & 2 deletions public/index.html
Expand Up @@ -67,8 +67,8 @@ <h1>Mirawatt</h1>
<div class="feedpicker">
<ul data-role="listview" data-theme="b" data-divider-theme="a" data-filter-theme="b" data-filter="true" data-inset="true" >
<!-- <li data-role="list-divider"><i>Select Feed</i></li> -->
<li data-icon="check"><a data-feed="sample" href="#">Sample</a></li>
<li data-icon="grid"><a data-feed="sampleBy2" href="#">SampleBy2</a></li>
<li data-icon="check"><a data-account="sample" href="#">Sample</a></li>
<li data-icon="grid"><a data-account="sampleBy2" href="#">SampleBy2</a></li>
</ul>
</div>
</div>
Expand Down
29 changes: 23 additions & 6 deletions public/js/app-feed.js
Expand Up @@ -3,7 +3,9 @@ var app = app || {};
(function(){
app.feed = {
toModel:toModel, // converts feeds and maps them to model data structures
fetchAll:fetchAll // fetchAll - get feeds (all scopes) - client initiated
lastFetch:null, // exposed so it can be reset from outside
fetchAll:fetchAll, // fetchAll - get feeds (all scopes) - client initiated
getAccounts:getAccounts // get a list of accountIds
};

function toModel(feed,callback){ // callback(summary,labels,data)
Expand Down Expand Up @@ -52,11 +54,10 @@ var app = app || {};
// fetchAll - get feeds (all scopes) - client initiated
// dependancies:
// uses app.svc, app.currentScope, app.accountId
// will throttle according to app.currentScope, and lastFetch
// will throttle according to app.currentScope, and app.feed.lastFetch
// options : {
// method: jsonrpc|dnode
// }
var lastFetch=null; // till we get push from dnode (reset fomr scope change)
function fetchAll(options,callback){ // callback(feeds)
if (!app.svc) {
// console.log('skip update - no connection yet');
Expand All @@ -68,12 +69,12 @@ var app = app || {};
},options);

// if scope is not Live, punt on update < 5,10 seconds
if (lastFetch){
var delay=+new Date()-lastFetch;
if (app.feed.lastFetch){
var delay=+new Date()-app.feed.lastFetch;
var minDelayForScope=[900,5000,10000,10000,10000][app.currentScope];
if (delay<minDelayForScope) return;
}
lastFetch=+new Date();
app.feed.lastFetch=+new Date();

if (options.method==='jsonrpc'){ // fetchFeeds-json
console.log('fetchFeeds-jsonrpc')
Expand All @@ -99,6 +100,22 @@ var app = app || {};
}
}

function getAccounts(callback){ // callback(accountIds)
if (!app.svc) {
// console.log('skip refreshAccounts - no connection');
return;
}
// this is the dnode fetch - all scopes
app.svc.accounts(function(err,accountIds){
if (err) {
console.log('dnode err',err);
return;
}
if (callback) callback(accountIds);
});
}


// jsonRPC invocation helper
var jsonRPCId=42; // jsonRPC invocation counter
function jsonRPC(method,paramsArray,successCB){
Expand Down
74 changes: 42 additions & 32 deletions public/js/app.js
@@ -1,4 +1,14 @@

// Application Context
var app = app || {};
app.svc=null;

// app.accountId is set on first refreshAccounts, when Dnode client is ready.
app.accountId = null; // 'sample';
app.currentScope=0;

setInterval(fetchFeeds, 1000);

// TODO: include initializr/plugin.js or RIM-boilerplate/common-utils.js - for console|log shim
// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/

Expand Down Expand Up @@ -36,6 +46,7 @@ function updateFromFeeds(feeds){
updateGraphFromCurrentModel();
}

// This is the client invocation of the feed fetch
function fetchFeeds(){
var options = {
// method: 'jsonrpc' // 'dnode'
Expand All @@ -45,38 +56,46 @@ function fetchFeeds(){
}

function refreshAccounts(){
if (!app.svc) {
// console.log('skip refreshAccounts - no connection');
return;
}
// this is the dnode fetch - all scopes
app.svc.accounts(function(err,accountIds){
app.feed.getAccounts(function(accountIds){
// console.log('accounts',accountIds);
if (app.accountId==null && accountIds && accountIds.length>0){
app.accountId=accountIds[0];
}
if (err) {
console.log('dnode err',err);
return;
changeAccount(accountIds[0]);
}
var $feedList = $('.feedpicker ul');
$feedList.html(''); // empty the list
$.each(accountIds,function(i,accountId){
var icon = (app.accountId===accountId)?'check':'grid';
$feedList.append('<li data-icon="'+icon+'"><a data-feed="'+accountId+'" href="#">'+accountId+'</a></li>');
$feedList.append('<li data-icon="'+icon+'"><a data-account="'+accountId+'" href="#">'+accountId+'</a></li>');
});
$feedList.listview('refresh');
});
}

setInterval(fetchFeeds, 3000);
function changeAccount(accountId){
console.log('changeAccount - clear Graphs?');
// TODO: reset the graphs.

var app = app || {};
app.svc=null;
app.currentScope=2;
// app.accountId is set on first refreshAccounts
app.accountId = null;// 'sample';
app.accountId = accountId;

app.feed.lastFetch=null; // till we get push from dnode
fetchFeeds();

console.log('about to subscribe',app.accountId,app.currentScope);
app.svc.subscribe({
accountId:app.accountId,
scopeId:app.currentScope
});

}

function changeScope(scopeId){
console.log('change scopeId',scopeId);
app.currentScope=scopeId%app.models.length;
app.feed.lastFetch=null; // till we get push from dnode
updateGraphFromCurrentModel();
}

// Application initialisation and bindings
$(function(){
hideURLBar();
$('html').bind('touchmove',function(e){
Expand All @@ -95,30 +114,21 @@ $(function(){
$('#home ul.scopepicker li').click(function(){
var scopeId = $(this).jqmData('scope-id');
if (typeof scopeId !='undefined'){
console.log('change scopeId',scopeId);
app.currentScope=scopeId%app.models.length;
lastFetch=null; // till we get push from dnode
updateGraphFromCurrentModel();
changeScope(scopeId);
}
});
$('#dygraph').click(function(){
app.currentScope=(app.currentScope+1)%app.models.length;
lastFetch=null; // till we get push from dnode
console.log('change scope',app.currentScope);
updateGraphFromCurrentModel();
var scopeId = app.currentScope+1; // will be modulo'd in changeScope
changeScope(scopeId);
});

$('.feedpickershow').click(function(){
refreshAccounts();
$('#home .feedpickerwrapper').toggleClass('showing');
});
$('.feedpicker li a').live('click',function(){
app.accountId=$(this).data('feed');
console.log('about to subscribe',app.accountId,app.currentScope);
app.svc.subscribe({
accountId:app.accountId,
scopeId:app.currentScope
});
var accountId = $(this).data('account');
changeAccount(accountId);
// console.log($('.feedpicker li'));
// $('.feedpicker li').attr('data-icon','home');
$('.feedpicker li span.ui-icon').removeClass('ui-icon-check')
Expand Down

0 comments on commit d93f5e5

Please sign in to comment.