Last active
August 29, 2015 14:03
-
-
Save marsen/e4e7b0b05885deaa9777 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
//how to use it ? | |
//1. You need jQuery | |
//2. finish the code about GetData GetTemplate GenSomething | |
//3. Call the Run() like the following | |
var obj = new myObject(); | |
obj.Run(); | |
// | |
function myObject() { | |
var template, | |
data = { | |
Channel: {}, | |
Article: {} | |
}, | |
deferreds = []; | |
this.Run = function () { | |
getData(getTemplate, getChannelData, genChannelPanel); | |
getData(getTemplate, getArticleData, genArticlePanel); | |
setEvent(); | |
}; | |
//Syntactic sugar | |
function getData(templateCallback, dataCallback, deferredDoneCallback) { | |
deferreds = []; | |
deferreds.push(templateCallback()); | |
deferreds.push(dataCallback()); | |
$.when.apply($, deferreds).done(deferredDoneCallback); | |
} | |
function genArticlePanel() { | |
//TODO SOMETHING WHEN YOU HAVE DATA & TEMPLATE | |
} | |
function getArticleData() { | |
return $.ajax({ | |
url: "xxx",//url to get data | |
type: "get",//get,post | |
data: {},//parameters | |
async: false,//true,false | |
cache: false,//true,false | |
dataType: "json",//text | |
success: function(items) { | |
//DO SOMETHING | |
data.Article = items; | |
} | |
}); | |
} | |
function genChannelPanel() { | |
//TODO SOMETHING WHEN YOU HAVE DATA & TEMPLATE | |
} | |
function getChannelData() { | |
return $.ajax({ | |
url: "xxx",//url to get data | |
type: "get",//get,post | |
data: {},//parameters | |
async: false,//true,false | |
cache: false,//true,false | |
dataType: "json", | |
success: function (items) { | |
//DO SOMETHING | |
data.Channel = items; | |
}, | |
error: function () { | |
} | |
}); | |
} | |
function getTemplate() { | |
if (template) { | |
// template is not null or undefined | |
return $.Deferred().resolve(); | |
} else { | |
console.log('template no cached'); | |
return $.ajax({ | |
url: "xxx",//url to get template | |
type: "get",//get,post | |
data: {},//parameters | |
async: false,//true,false | |
cache: false,//true,false | |
dataType: "json", | |
success: function (items) { | |
//DO SOMETHING | |
template = items; | |
}, | |
error: function () { | |
//DO SOMETHING | |
} | |
}); | |
} | |
} | |
function setEvent() { | |
//DO SOMETHING | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment