Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 3 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
Commits on Apr 10, 2012
@rmurphey standard interface to data stuff e6fd4c3
Commits on Apr 13, 2012
@rmurphey favorites wip 03c95a2
@rmurphey Merge branch 'favorites' of github.com:rmurphey/srchr-demo into favor…
…ites

* 'favorites' of github.com:rmurphey/srchr-demo:
  cleanup
061eee0
View
10 app/controllers/search.js
@@ -1,11 +1,10 @@
define([
'models/app',
- 'data/search',
'components/results',
'components/searchForm',
'components/recentSearches',
'components/user'
-], function(app, SearchData, ResultsComponent, SearchFormComponent, RecentSearchesComponent, UserComponent) {
+], function(app, ResultsComponent, SearchFormComponent, RecentSearchesComponent, UserComponent) {
return function(term) {
var mainbar = $('#mainbar').empty(),
sidebar = $('#sidebar').empty(),
@@ -13,7 +12,7 @@ define([
sf = new SearchFormComponent().render().placeAt(mainbar),
results = new ResultsComponent({
- searchData : SearchData
+ searchData : app.searchData
}).render().placeAt(mainbar),
user = new UserComponent({
@@ -25,14 +24,13 @@ define([
currentSearch : app.currentSearch
}).render().placeAt(sidebar);
-
sf.on('search', function(term) {
window.Router.navigate('/search/' + term, true);
});
app.currentSearch.on('change', function(s) {
- SearchData.term = s.get('term');
- SearchData.fetch();
+ app.searchData.term = s.get('term');
+ app.searchData.fetch();
});
update(term);
View
16 app/data/favorites.js
@@ -0,0 +1,16 @@
+define([
+ 'use!backbone'
+], function(B) {
+ var Favorite = Backbone.Model.extend({
+
+ });
+
+ var Favorites = Backbone.Collection.extend({
+ model : Favorite,
+ url : function() {
+ return '/favorites/' + this.user.get('name') + '/';
+ }
+ });
+
+ return Favorites;
+});
View
2  app/data/search.js
@@ -13,5 +13,5 @@ define([
}
});
- return new SearchResults();
+ return SearchResults;
});
View
19 app/data/searches.js
@@ -0,0 +1,19 @@
+define([
+ 'use!backbone'
+], function(B) {
+ var Search = B.Model.extend({
+ idAttribute : 'term'
+ });
+
+ var Searches = B.Collection.extend({
+ comparator : function(item) {
+ return item.get('time') * -1;
+ },
+ model : Search,
+ store : function(item) {
+ window.localStorage.setItem('searches', JSON.stringify(this.toJSON()));
+ }
+ });
+
+ return Searches;
+});
View
42 app/models/app.js
@@ -1,26 +1,34 @@
define([
- 'use!backbone'
-], function(B) {
- var Search = B.Model.extend({
- idAttribute : 'term'
- });
+ 'use!backbone',
+ 'data/favorites',
+ 'data/searches',
+ 'data/search',
+ 'models/user'
+], function(B, Favorites, Searches, SearchData, User) {
+ var searches = new Searches(
+ JSON.parse(
+ window.localStorage.getItem('searches')
+ )
+ ),
- var Searches = B.Collection.extend({
- comparator : function(item) {
- return item.get('time') * -1;
- },
- model : Search,
- store : function(item) {
- window.localStorage.setItem('searches', JSON.stringify(this.toJSON()));
- }
- });
+ user = new User(),
+
+ favorites = new Favorites({ user : user }),
+
+ search = new B.Model({ term : null }),
+
+ searchData = new SearchData();
- var searches = new Searches(JSON.parse(window.localStorage.getItem('searches')));
searches.on('add remove', searches.store);
+ user.on('change', function(u) {
+ favorites.username = u.get('name');
+ });
return {
+ searchData : searchData,
+ favorites : favorites,
searches : searches,
- currentSearch : new Backbone.Model({ term : null }),
- currentUser : new Backbone.Model({ name : null })
+ currentSearch : search,
+ currentUser : user
};
});
View
16 app/models/user.js
@@ -0,0 +1,16 @@
+define([
+ 'use!backbone'
+], function(B) {
+ var User = B.Model.extend({
+ initialize : function() {
+ this.set('name', window.localStorage.getItem('username'));
+ this.on('change', this._store);
+ },
+
+ _store : function() {
+ window.localStorage.setItem('username', this.get('name'));
+ }
+ });
+
+ return User;
+});
View
10 server/server.js
@@ -39,28 +39,28 @@ module.exports = function(opts) {
);
});
- site.post("/favorites", function(req, res) {
+ site.post("/favorites/:username", function(req, res) {
var fav = JSON.parse(req.body.favorite);
var id = favs.add(fav);
res.end(JSON.stringify({ id : id }));
});
- site.put("/favorites/:id", function(req, res) {
+ site.put("/favorites/:username/:id", function(req, res) {
var fav = JSON.parse(req.body.favorite);
var id = favs.update(req.params.id, fav);
res.end(JSON.stringify({ id : id }));
});
- site.delete("/favorites/:id", function(req, res) {
+ site.delete("/favorites/:username/:id", function(req, res) {
favs.remove(req.params.id);
res.end(JSON.stringify({ success : true }));
});
- site.get("/favorites", function(req, res) {
+ site.get("/favorites/:username", function(req, res) {
res.end(JSON.stringify(favs.get()));
});
- site.get("/favorites/:id", function(req, res) {
+ site.get("/favorites/:username/:id", function(req, res) {
var fav = favs.get(req.params.id);
res.end(JSON.stringify(fav));
});

No commit comments for this range

Something went wrong with that request. Please try again.