Last active
August 29, 2015 13:57
-
-
Save bradoyler/9541200 to your computer and use it in GitHub Desktop.
example routes and controllers
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
App.Router.map(function() { | |
this.route("news", { | |
path: "/news" | |
}), this.route("entertainment", { | |
path: "/entertainment" | |
}), this.route("fashionBeauty", { | |
path: "/fashion-beauty" | |
}), this.route("lifestyle", { | |
path: "/lifestyle" | |
}), this.route("books", { | |
path: "/books" | |
}), this.resource("embeddedSlideshow", { | |
path: "/articles/:embeddedSlug" | |
}, function() { | |
this.route("slide", { | |
path: "/:slideNumber" | |
}) | |
}), this.resource("articles", { | |
path: "/articles/:slug" | |
}, function() { | |
this.route("slideshow", { | |
path: "/:slideNumber" | |
}), this.route("image", { | |
path: "/image/:attachmentId" | |
}) | |
}), this.route("explore", { | |
path: "/explore" | |
}), this.route("exploreSubject", { | |
path: "/explore/:slug" | |
}), this.route("authors", { | |
path: "/authors/:slug" | |
}), this.route("terms", { | |
path: "/tos" | |
}), this.route("privacy", { | |
path: "/privacy" | |
}), this.route("contact", { | |
path: "/contact" | |
}), this.route("dmca", { | |
path: "/dmca" | |
}), this.route("about", { | |
path: "/about" | |
}), this.route("errorPage", { | |
path: "/*path" | |
}) | |
}), App.Route = Ember.Route.extend(Ember.RouteMeta, { | |
returnToTop: function() { | |
this.router.sequence > 0 && Ember.run.scheduleOnce("afterRender", this, function() { | |
window.scrollTo(0, 0) | |
}) | |
}, | |
fetchPreloadedModelForClass: function(e) { | |
var t = App.get("preloadedPageModel"); | |
return e && e.detectInstance(t) ? (App.set("preloadedPageModel", null), t) : null | |
}, | |
onMetaRendered: function() { | |
App.Analytics.tracker.trackMeta(null) | |
} | |
}), App.ApplicationRoute = Ember.Route.extend({ | |
actions: { | |
loading: function() { | |
this.controllerFor("application").set("isPageLoading", !0), this.router.one("didTransition", this, function() { | |
this.controllerFor("application").set("isPageLoading", !1) | |
}) | |
}, | |
error: function(e, t) { | |
if (e && 0 !== e.status) { | |
var n = this.router.router.recognizer.generate(t.targetName); | |
n && (e.intendedPath = n.substring(1)), this.transitionTo("errorPage", e) | |
} | |
}, | |
openSearch: function() { | |
this.render("searchLightbox", { | |
into: "application", | |
outlet: "lightbox", | |
controller: "search" | |
}), App.Analytics.tracker.trackPageView("/search") | |
}, | |
closeSearch: function() { | |
this.disconnectOutlet({ | |
outlet: "lightbox", | |
parentView: "application" | |
}) | |
}, | |
trackPaginatedPageView: function(e) { | |
App.Analytics.tracker.trackPageView(this.get("router.url").replace(/\/$/, "") + "/page/" + e) | |
} | |
} | |
}), App.IndexRoute = App.Route.extend(Ember.CachedRoute, { | |
controllerName: "homePage", | |
titlePath: null, | |
model: function(e, t) { | |
return this.getCachedModelForTransition(t) || this.fetchPreloadedModelForClass(App.Section) || App.Section.fetch("home") | |
}, | |
setupController: function(e, t) { | |
e.set("content", t), this.controllerFor("carousel").set("content", e.get("carouselContent")) | |
}, | |
renderTemplate: function() { | |
this.render("home"), this.render("carousel", { | |
into: "home", | |
outlet: "carousel", | |
controller: "carousel" | |
}), this.isUsingCachedModel || this.returnToTop() | |
}, | |
onMetaRendered: function(e, t) { | |
App.Analytics.tracker.trackMeta({ | |
sections: e.get("title") | |
}), window.MetaManager.setSocialMediaTags({ | |
twitter: { | |
"twitter:card": "summary" | |
}, | |
openGraph: { | |
"og:description": t.description | |
} | |
}) | |
} | |
}), | |
App.SectionPageController = Ember.Controller.extend(App.PaginatedContentController, { | |
featuredContentPart1Count: 4, | |
showcaseContent: function() { | |
var e, t, n = this.get("content.modules"); | |
return n && (e = n.filterProperty("position", "showcase"), e && e.objectAt(0) && (t = e.objectAt(0).get("objects"))) ? t.slice(0, 2) : null | |
}.property("content.modules.@each"), | |
sidebarModules: function() { | |
var e = this.get("content.modules"); | |
return e ? e.filterProperty("position", "sidebar") : null | |
}.property("content.modules.@each"), | |
featuredContentModule: function() { | |
var e, t = this.get("content.modules"); | |
return t && (e = t.filterProperty("position", "featured")) ? e.objectAt(0) : null | |
}.property("content.modules.@each"), | |
featuredContentModuleId: Ember.computed.alias("featuredContentModule.title"), | |
featuredContent: Ember.computed.alias("featuredContentModule.objects"), | |
featuredContentPart1: function() { | |
var e = this.get("featuredContent"); | |
return e ? e.slice(0, this.get("featuredContentPart1Count")) : null | |
}.property("featuredContent"), | |
featuredContentPart2: function() { | |
var e = this.get("featuredContent"); | |
return e ? e.slice(this.get("featuredContentPart1Count")) : null | |
}.property("featuredContent.length"), | |
perPage: 24, | |
maxPages: 10, | |
setupPaginationOnInitialContent: function(e, t) { | |
var n, r = e.get(t); | |
r && (n = r.get("pagination"), n || r.set("pagination", App.Pagination.create({ | |
pageCount: this.get("featuredContentModule.meta.pagination.page_count") || 1 | |
}))) | |
}.observes("content"), | |
fetchPageMethod: function() { | |
var e = this.get("content.slug"), | |
t = { | |
exclude_section: e, | |
page: this.get("nextPage"), | |
per_page: this.get("perPage"), | |
state: App.Article.states.PUBLISHED | |
}; | |
"home" !== e && (t.category = App.Category.getBySlug(e).get("id")), App.Article.fetch(t).then(this.onFetchPageSuccess.bind(this), this.onFetchPageError.bind(this)) | |
}, | |
onFetchPageSuccess: function(e) { | |
this._super.apply(this, arguments), e && this.get("featuredContent").addObjects(e) | |
} | |
}), | |
///------------- | |
// Example ----- | |
///------------- | |
App.SectionPageRoute = App.Route.extend(Ember.CachedRoute, { | |
category: null, | |
controllerName: "sectionPage", | |
model: function(controller, model) { | |
return this.getCachedModelForTransition(model) || this.fetchPreloadedModelForClass(App.Section) || App.Section.fetch(this.category.get("slug")) | |
}, | |
setupController: function(controller, model) { | |
controller.setProperties({ | |
content: model, | |
category: this.category | |
}) | |
}, | |
renderTemplate: function() { | |
this.render("section"), this.isUsingCachedModel || this.returnToTop() | |
}, | |
onMetaRendered: function(controller, model) { | |
App.Analytics.tracker.trackMeta({ | |
sections: controller.get("title") | |
}), window.MetaManager.setSocialMediaTags({ | |
twitter: { | |
"twitter:card": "summary" | |
}, | |
openGraph: { | |
"og:title": model.title, | |
"og:description": model.description | |
} | |
}) | |
} | |
}), | |
//////////// | |
App.NewsRoute = App.SectionPageRoute.extend({ | |
category: App.Category.categories[0], | |
title: App.Category.categories[0].title | |
}), | |
/////// | |
App.EntertainmentRoute = App.SectionPageRoute.extend({ | |
category: App.Category.categories[1], | |
title: App.Category.categories[1].title | |
}), App.FashionBeautyRoute = App.SectionPageRoute.extend({ | |
category: App.Category.categories[2], | |
title: App.Category.categories[2].title | |
}), App.LifestyleRoute = App.SectionPageRoute.extend({ | |
category: App.Category.categories[3], | |
title: App.Category.categories[3].title | |
}), App.BooksRoute = App.SectionPageRoute.extend({ | |
category: App.Category.categories[4], | |
title: App.Category.categories[4].title | |
}), App.ExploreRoute = App.Route.extend(Ember.CachedRoute, { | |
title: "Explore", | |
controllerName: "explorePage", | |
model: function(e, t) { | |
return this.getCachedModelForTransition(t) || App.Article.fetch({ | |
per_page: 24, | |
state: App.Article.states.PUBLISHED | |
}) | |
}, | |
setupController: function(e, t) { | |
e.setProperties({ | |
content: t, | |
category: null, | |
tag: null | |
}) | |
}, | |
renderTemplate: function() { | |
this.render("explorePage"), this.isUsingCachedModel || this.returnToTop() | |
} | |
}), App.ExploreSubjectRoute = App.ExploreRoute.extend({ | |
category: null, | |
tag: null, | |
title: Ember.computed(function() { | |
var e = this.get("category.title"), | |
t = this.get("tag.name"), | |
n = "Explore "; | |
return e ? n += e : t && (n += "'" + Ember.String.capitalize(t) + "'"), n | |
}).property("category", "tag"), | |
beforeModel: function(e) { | |
this.set("category", App.Category.getBySlug(e.params.slug) || null) | |
}, | |
model: function(e, t) { | |
var n = this.getCachedModelForTransition(t), | |
r = this.get("category"), | |
i = this; | |
return n ? n : r ? App.Article.fetch({ | |
per_page: 24, | |
category: r.get("id"), | |
state: App.Article.states.PUBLISHED | |
}) : App.Tag.fetch(e.slug).then(function(e) { | |
return i.set("tag", e), App.Article.fetch({ | |
per_page: 24, | |
tag: e.get("slug"), | |
state: App.Article.states.PUBLISHED | |
}) | |
}) | |
}, | |
setupController: function(e, t) { | |
var n = this.get("category"), | |
r = this.get("tag"); | |
e.setProperties({ | |
content: t, | |
category: n ? n : null, | |
tag: r ? r : null | |
}) | |
}, | |
renderTemplate: function() { | |
var e = this.get("category") ? "exploreCategoryPage" : "exploreTagPage"; | |
this.render(e), this.isUsingCachedModel || this.returnToTop() | |
}, | |
actions: { | |
error: function(e) { | |
404 === e.status && this.transitionTo("errorPage", { | |
status: 404, | |
errorThrown: "Not Found" | |
}) | |
} | |
} | |
}), App.ArticlesRoute = App.Route.extend(Ember.CachedRoute, { | |
controllerName: "articlePage", | |
descriptionPath: "body", | |
serialize: function(e) { | |
return e ? { | |
slug: e.get("slug") | |
} : null | |
}, | |
model: function(e, t) { | |
var n = this.getCachedModelForTransition(t, e); | |
if (n) return n; | |
var r = this.fetchPreloadedModelForClass(App.Article); | |
if (r) return r; | |
if (e && e.slug) { | |
var i = parseInt(e.slug, 10); | |
if (!isNaN(i)) return App.Article.fetch(i) | |
} | |
this.replaceWith("errorPage", { | |
status: 404, | |
errorThrown: "Not Found" | |
}) | |
}, | |
setupController: function(e, t) { | |
e.set("content", t), e.sideloadArticle() | |
}, | |
renderTemplate: function() { | |
this.render("articlePage"), this.isUsingCachedModel || this.returnToTop() | |
}, | |
onMetaRendered: function(e, t) { | |
if (t.title && t.description || t.title && !t.description) { | |
App.Analytics.tracker.trackMeta({ | |
authors: e.get("author.name"), | |
sections: e.get("categoryName"), | |
date: e.get("publishedAt"), | |
tags: e.get("tags").map(function(e) { | |
return e.get("name") | |
}) | |
}); | |
var n = App.ImageView.addParams(e.get("mediaUrl"), App.ImageView.presets.shareArticle), | |
r = e.get("author.twitter"); | |
window.MetaManager.setSocialMediaTags({ | |
twitter: { | |
"twitter:card": "summary_large_image", | |
"twitter:creator": r ? "@" + r : null, | |
"twitter:app:url:iphone": e.get("iOSAppUrl") | |
}, | |
openGraph: { | |
"og:type": "article", | |
"og:title": e.get("title"), | |
"og:image": n | |
} | |
}) | |
} | |
t.description && window.MetaManager.addOpenGraphTags({ | |
"og:description": t.description | |
}) | |
}, | |
actions: { | |
openImageLightbox: function(e) { | |
e && this.transitionTo("articles.image", e) | |
}, | |
updateRouteOnLightboxClose: function() { | |
this.disconnectOutlet({ | |
outlet: "lightbox", | |
parentView: "application" | |
}), this.router.sequence <= 1 ? this.replaceWith("articles") : history.back() | |
} | |
} | |
}), App.ArticlesIndexRoute = App.Route.extend({ | |
controllerName: "articlePage" | |
}), App.ArticlesSlideshowRoute = App.Route.extend({ | |
slide: null, | |
controllerName: "slideshow", | |
descriptionPath: "body", | |
model: function(e) { | |
var t = parseInt(e.slideNumber, 10) || 1, | |
n = this.modelFor("articles"); | |
if (this.controllerFor("slideshow").set("slideNumber", t), !n || !n.get("isFullyLoaded")) return this.controllerFor("slideshow").sideloadArticle(); | |
var r = n.get("slides").objectAt(t - 1); | |
return r ? (this.set("slide", r), n) : (this.replaceWith("articles"), void 0) | |
}, | |
renderTemplate: function() { | |
this.render("slideshowPage", { | |
into: "application", | |
outlet: "lightbox" | |
}) | |
}, | |
onMetaRendered: function(e, t) { | |
var n = App.ImageView.addParams(e.get("mediaUrl"), App.ImageView.presets.shareImage), | |
r = this.modelFor("articles"), | |
i = r.get("author.twitter"), | |
s = this.get("slide.title") + " | " + e.get("title"); | |
App.Analytics.tracker.trackMeta({ | |
authors: e.get("author.name"), | |
sections: e.get("categoryName"), | |
date: e.get("publishedAt"), | |
tags: e.get("tags").map(function(e) { | |
return e.get("name") | |
}), | |
landingUrl: location.protocol + "//" + location.hostname + this.get("router").generate("articles.index") | |
}), window.MetaManager.setTitleAndDescription(s, this.get("slide.body")), window.MetaManager.setSocialMediaTags({ | |
twitter: { | |
"twitter:card": "photo", | |
"twitter:creator": i ? "@" + i : null, | |
"twitter:app:url:iphone": r.get("iOSAppUrl") | |
}, | |
openGraph: { | |
"og:type": "article", | |
"og:title": e.get("title"), | |
"og:description": t.description, | |
"og:image": n | |
} | |
}) | |
}, | |
deactivate: function() { | |
App.SlideshowPageView.closeViaRouter(), this.controllerFor("slideshow").reset() | |
}, | |
actions: { | |
gotoSlide: function(e) { | |
this.replaceWith(this.routeName, e) | |
} | |
} | |
}), App.EmbeddedSlideshowRoute = App.ArticlesSlideshowRoute.extend({ | |
afterModel: function(e) { | |
e.set("primaryCategory", this.modelFor("articles").get("primaryCategory")) | |
}, | |
serialize: function(e) { | |
return { | |
embeddedSlug: e.get("slug") | |
} | |
}, | |
renderTemplate: function() { | |
this.render("articlePage", { | |
controller: "articlePage" | |
}), this.render("slideshowPage", { | |
into: "application", | |
outlet: "lightbox" | |
}) | |
}, | |
actions: { | |
updateRouteOnLightboxClose: function() { | |
this.disconnectOutlet({ | |
outlet: "lightbox", | |
parentView: "application" | |
}), history.back() | |
} | |
} | |
}), App.EmbeddedSlideshowSlideRoute = App.Route.extend({ | |
controllerName: "slideshow", | |
model: function(e) { | |
var t = parseInt(e.slideNumber, 10) || 1; | |
return this.controllerFor("slideshow").set("slideNumber", t), this.modelFor("embeddedSlideshow") | |
}, | |
actions: { | |
gotoSlide: function(e) { | |
this.replaceWith(this.routeName, e) | |
} | |
} | |
}), App.ArticlesImageRoute = App.Route.extend({ | |
controllerName: "imageLightbox", | |
serialize: function(e) { | |
if (e) { | |
var t = e.get("attachmentId"); | |
if (!Ember.isNone(t)) return { | |
attachmentId: t | |
} | |
} | |
return null | |
}, | |
model: function(e) { | |
return this.modelFor("articles"), e && e.attachmentId ? e.attachmentId : (this.replaceWith("articles"), void 0) | |
}, | |
setupController: function(e, t) { | |
e.set("attachmentId", t) | |
}, | |
renderTemplate: function() { | |
this.render("imageLightbox", { | |
into: "application", | |
outlet: "lightbox" | |
}) | |
}, | |
deactivate: function() { | |
App.LightboxView.closeViaRouter() | |
}, | |
onMetaRendered: function() { | |
var e = "Photo: " + this.modelFor("articles").get("title"), | |
t = "High-Resolution Image: " + this.modelFor("articles").get("body"); | |
window.MetaManager.setTitleAndDescription(e, t) | |
}, | |
actions: { | |
changeImage: function(e) { | |
this.replaceWith("articles.image", e) | |
} | |
} | |
}), App.AuthorsRoute = App.Route.extend(Ember.CachedRoute, { | |
controllerName: "authorPage", | |
titlePath: "name", | |
descriptionPath: "bio", | |
serialize: function(e) { | |
return e ? { | |
slug: e.get("slug") | |
} : null | |
}, | |
model: function(e, t) { | |
var n = this.getCachedModelForTransition(t, e); | |
if (n) return n; | |
var r = this.fetchPreloadedModelForClass(App.Author); | |
if (r) return r; | |
if (e && e.slug) { | |
var i = parseInt(e.slug, 10); | |
if (!isNaN(i)) return App.Author.fetch(i) | |
} | |
this.replaceWith("errorPage", { | |
status: 404, | |
errorThrown: "Not Found" | |
}) | |
}, | |
setupController: function(e, t) { | |
e.set("content", t), e.sideloadProfile() | |
}, | |
renderTemplate: function() { | |
this.render("authorPage"), this.isUsingCachedModel || this.returnToTop() | |
} | |
}), App.StaticPageRoute = App.Route.extend(Ember.CachedRoute, { | |
controllerName: "staticPage", | |
model: function(e, t) { | |
return this.getCachedModelForTransition(t) || this.fetchPreloadedModelForClass(App.Page) || App.Page.fetch(this.page.get("slug")) | |
}, | |
renderTemplate: function() { | |
this.render("staticPage"), this.isUsingCachedModel || this.returnToTop() | |
} | |
}), App.TermsRoute = App.StaticPageRoute.extend({ | |
page: App.Page.staticPages[0], | |
title: App.Page.staticPages[0].title | |
}), App.PrivacyRoute = App.StaticPageRoute.extend({ | |
page: App.Page.staticPages[1], | |
title: App.Page.staticPages[1].title | |
}), App.ContactRoute = App.StaticPageRoute.extend({ | |
page: App.Page.staticPages[3], | |
title: App.Page.staticPages[3].title | |
}), App.DmcaRoute = App.StaticPageRoute.extend({ | |
page: App.Page.staticPages[4], | |
title: App.Page.staticPages[4].title | |
}), App.AboutRoute = App.Route.extend({ | |
title: "About", | |
controllerName: "about", | |
beforeModel: function() { | |
return Ember.LazyAssetLoader.load("about", ["//d3sdf3l7p4rfar.cloudfront.net/assets/pods/about-92bd2db2432a75f07f6eb6c5fc2b2294.css", "//d3sdf3l7p4rfar.cloudfront.net/assets/content/pods/about-abc0dca534d8fd43a707afb8b08734d8.js"]) | |
}, | |
model: function() { | |
return App.AboutPage.find(1) | |
}, | |
setupController: function(e, t) { | |
e.set("content", t) | |
} | |
}), App.ErrorPageRoute = App.Route.extend({ | |
title: "Error", | |
serialize: function(e) { | |
var t = e && e.intendedPath || ""; | |
return { | |
path: t | |
} | |
}, | |
renderTemplate: function() { | |
this.render("errorPage"), this.returnToTop() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment