Skip to content

Instantly share code, notes, and snippets.

@marsen
Last active August 29, 2015 14:03
Show Gist options
  • Save marsen/e4e7b0b05885deaa9777 to your computer and use it in GitHub Desktop.
Save marsen/e4e7b0b05885deaa9777 to your computer and use it in GitHub Desktop.
//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