From 838da1fd52b7fdf7896f588f304a58e3604b960b Mon Sep 17 00:00:00 2001
From: Maxwell Salzberg <maxwell@joindiaspora.com>
Date: Thu, 19 Jan 2012 14:20:31 -0800
Subject: [PATCH] Revert "refactoring backbone urls"

This reverts commit 4ad58bdf2f97678cf4d963bdc136306dff2e95fc.
---
 .../javascripts/app/collections/comments.js   |  6 +---
 public/javascripts/app/collections/likes.js   |  6 +---
 public/javascripts/app/models/post.js         | 30 ++++++++++++++-----
 public/javascripts/app/views/feedback_view.js |  2 +-
 .../collections/comments_collection_spec.js   | 10 -------
 .../app/collections/likes_collections_spec.js | 10 -------
 spec/javascripts/app/models/post_spec.js      |  9 ------
 7 files changed, 25 insertions(+), 48 deletions(-)
 delete mode 100644 spec/javascripts/app/collections/comments_collection_spec.js
 delete mode 100644 spec/javascripts/app/collections/likes_collections_spec.js

diff --git a/public/javascripts/app/collections/comments.js b/public/javascripts/app/collections/comments.js
index d564f86825..03fc518cbf 100644
--- a/public/javascripts/app/collections/comments.js
+++ b/public/javascripts/app/collections/comments.js
@@ -1,7 +1,3 @@
 app.collections.Comments = Backbone.Collection.extend({
-  model: app.models.Comment,
-
-  initialize : function(models, options) {
-    this.url = "/posts/" + options.post.id + "/comments" //not delegating to post.url() because when it is in a stream collection it delegates to that url
-  }
+  model: app.models.Comment
 });
diff --git a/public/javascripts/app/collections/likes.js b/public/javascripts/app/collections/likes.js
index 01831d4f95..d576b8e595 100644
--- a/public/javascripts/app/collections/likes.js
+++ b/public/javascripts/app/collections/likes.js
@@ -1,7 +1,3 @@
 app.collections.Likes = Backbone.Collection.extend({
-  model: app.models.Like,
-
-  initialize : function(models, options) {
-    this.url = "/posts/" + options.post.id + "/likes" //not delegating to post.url() because when it is in a stream collection it delegates to that url
-  }
+  model: app.models.Like
 });
diff --git a/public/javascripts/app/models/post.js b/public/javascripts/app/models/post.js
index 8ab106e491..b9641156d1 100644
--- a/public/javascripts/app/models/post.js
+++ b/public/javascripts/app/models/post.js
@@ -1,16 +1,21 @@
 app.models.Post = Backbone.Model.extend({
-  urlRoot : "/posts",
   initialize : function() {
-    this.comments = new app.collections.Comments(this.get("last_three_comments"), {post : this});
-    this.likes = new app.collections.Likes(this.get("user_like"), { post : this}); // load in the user like initially
-  },
+    this.comments = new app.collections.Comments(this.get("last_three_comments"));
+    this.comments.url = this.url() + '/comments';
 
-  createdAt : function() {
-    return new Date(this.get("created_at")) / 1000;
+    this.likes = new app.collections.Likes(this.get("user_like")); // load in the user like initially
+    this.likes.url = this.url() + '/likes';
   },
 
-  createReshareUrl : "/reshares",
+  url : function() {
+    if(this.id) {
+      return "/posts/" + this.id;
+    } else {
+      return "/posts"
+    }
+  },
 
+  reshareUrl : "/reshares",
   reshare : function(){
     return this._reshare = this._reshare || new app.models.Reshare({root_guid : this.get("guid")});
   },
@@ -28,8 +33,17 @@ app.models.Post = Backbone.Model.extend({
     }
   },
 
+  createdAt : function() {
+    return new Date(this.get("created_at")) / 1000;
+  },
+
+
+  likeUrl : function(){
+    return this.url() + "/likes"
+  },
+
   like : function() {
-    this.set({ user_like : this.likes.create() });
+    this.set({ user_like : this.likes.create({}, {url : this.likeUrl()}) });
   },
 
   unlike : function() {
diff --git a/public/javascripts/app/views/feedback_view.js b/public/javascripts/app/views/feedback_view.js
index 84c1ced120..68dc013a14 100644
--- a/public/javascripts/app/views/feedback_view.js
+++ b/public/javascripts/app/views/feedback_view.js
@@ -18,7 +18,7 @@ app.views.Feedback = app.views.StreamObject.extend({
     if(!window.confirm("Reshare " + this.model.reshareAuthor().name + "'s post?")) { return }
     var reshare = this.model.reshare()
     reshare.save({}, {
-      url: this.model.createReshareUrl,
+      url: this.model.reshareUrl,
       success : function(){
         app.stream.collection.add(reshare);
       }
diff --git a/spec/javascripts/app/collections/comments_collection_spec.js b/spec/javascripts/app/collections/comments_collection_spec.js
deleted file mode 100644
index 7c94d45014..0000000000
--- a/spec/javascripts/app/collections/comments_collection_spec.js
+++ /dev/null
@@ -1,10 +0,0 @@
-describe("app.collections.comments", function(){
-  describe("url", function(){
-    it("should user the post id", function(){
-      var post =new app.models.Post({id : 5})
-      var collection = new app.collections.Comments([], {post : post})
-      expect(collection.url).toBe("/posts/5/comments")
-    })
-  })
-})
-
diff --git a/spec/javascripts/app/collections/likes_collections_spec.js b/spec/javascripts/app/collections/likes_collections_spec.js
deleted file mode 100644
index 2928884036..0000000000
--- a/spec/javascripts/app/collections/likes_collections_spec.js
+++ /dev/null
@@ -1,10 +0,0 @@
-describe("app.collections.Likes", function(){
-  describe("url", function(){
-    it("should user the post id", function(){
-      var post =new app.models.Post({id : 5})
-      var collection = new app.collections.Likes([], {post : post})
-      expect(collection.url).toBe("/posts/5/likes")
-    })
-  })
-})
-
diff --git a/spec/javascripts/app/models/post_spec.js b/spec/javascripts/app/models/post_spec.js
index 4231096ae3..ed5aede9fd 100644
--- a/spec/javascripts/app/models/post_spec.js
+++ b/spec/javascripts/app/models/post_spec.js
@@ -3,15 +3,6 @@ describe("app.models.Post", function() {
     this.post = new app.models.Post();
   })
 
-  describe("url", function(){
-    it("should be /posts when it doesn't have an id", function(){
-      expect(new app.models.Post().url()).toBe("/posts")
-    })
-  
-    it("should be /posts/id when it doesn't have an id", function(){
-      expect(new app.models.Post({id: 5}).url()).toBe("/posts/5")
-    })
-  })
   describe("createdAt", function() {
     it("returns the post's created_at as an integer", function() {
       var date = new Date;
-- 
GitLab