Created
October 14, 2012 01:27
-
-
Save aezell/3886893 to your computer and use it in GitHub Desktop.
Backbone Attempt
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
var app = app || {}; | |
$(function(){ | |
var app.Signature = Backbone.Model.extend({ | |
initialize: function(){ | |
return $.ajax({ | |
url: '/auth/' | |
}); | |
} | |
}); | |
var app.Campaign = Backbone.Model.extend({ | |
getPublications: function(){ | |
var campaignPublications = new Publications(); | |
var signaturePromise = new app.Signature(); | |
signaturePromise.done(function(signature){ | |
var date_append = ''; | |
var now = new Date(); | |
var today = now.toISOString().slice(0,10); | |
date_append = 'metrics=true&sparse=false&endDate=' + today; | |
var url = 'https://someserver.com/1/campaigns/' + 2 + '/publications.json?'; | |
url = url + date_append; | |
url = url + '&' + signature; | |
var publicationsRetrieval = $.ajax({ | |
url: url, | |
dataType: 'json' | |
}); | |
publicationsRetrieval.done(function(response){ | |
if (response.publications.length < 1) { | |
campaignPublications.reset(); | |
} | |
else { | |
response.publications.forEach(function(publication){ | |
if(is_posted(publication.post_at)) { | |
campaignPublications.add(publication); | |
} | |
}, this); | |
} | |
}); | |
}); | |
return this.campaignPublications; | |
}, | |
list: function(){ | |
var campaigns = new app.Campaigns(); | |
var signaturePromise = new app.Signature(); | |
signaturePromise.done(function(signature){ | |
var url = 'https://someserver.com/1/campaigns/list.json?'; | |
url = url + signature; | |
var campaignsRetrieval = $.ajax({ | |
url: url, | |
dataType: 'json' | |
}); | |
campaignsRetrieval.done(function(response){ | |
response.data.own.forEach(function(campaign){ | |
campaigns.add(campaign); | |
}, this); | |
}); | |
}); | |
return this.campaigns; | |
} | |
}); | |
var Publication = Backbone.Model.extend({ | |
list: function(posted){ | |
var postedPublications = new app.Publications(); | |
var scheduledPublications = new app.Publications(); | |
var signaturePromise = new app.Signature(); | |
signaturePromise.done(function(signature){ | |
var date_append = ''; | |
var now = new Date(); | |
var today = now.toISOString().slice(0,10); | |
if (posted) { | |
date_append = 'metrics=true&endDate=' + today; | |
} | |
else { | |
date_append = 'metrics=false&startDate=' + today; | |
} | |
var url = 'https://someserver.com/1/publications/list.json?'; | |
url = url + date_append; | |
url = url + '&' + signature; | |
var publicationRetrieval = $.ajax({ | |
url: url, | |
dataType: 'json' | |
}); | |
publicationRetrieval.done(function(response){ | |
response.publications.forEach(function(publication){ | |
if (posted) { | |
postedPublications.add(publication); | |
} else { | |
scheduledPublications.add(publication); | |
} | |
}, this); | |
}); | |
}); | |
if (posted) { | |
return this.postedPublications; | |
} else { | |
return this.scheduledPublications; | |
} | |
} | |
}); | |
var app.PostedPublications = Backbone.Collection.extend({ | |
model: app.Publication | |
}); | |
var app.ScheduledPublications = Backbone.Collection.extend({ | |
model: app.Publication | |
}); | |
var app.Campaigns = Backbone.Collection.extend({ | |
model: app.Campaign, | |
initialize: function (models, options) { | |
this.campaigns = app.Campaign.list(); | |
} | |
}); | |
var app.PublishedCampaigns = Backbone.View.extend({ | |
initialize: function () { | |
this.campaigns = new Campaigns( null, { view: this }); | |
} | |
render: function() { | |
var template = Handlebars.compile($("#published-campaign-select").html()); | |
console.log(this.campaigns); | |
$(this.el).html(template({ campaigns: this.campaigns})); | |
return this; | |
} | |
}); | |
function is_posted(post_at){ | |
var post_date = new Date(post_at); | |
return post_date < new Date(); | |
} | |
var pub_campaigns = new app.PublishedCampaigns(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment