Browse code

Backbone s podporou websocket

Cinan Rakosnik authored on 11/03/2013 at 21:30:38
Showing 27 changed files
... ...
@@ -5,4 +5,4 @@ You are allowed to:
5 5
 2. Remove generators
6 6
 3. Add installed generators
7 7
 To add new installed generators automatically delete this file and reload the project.
8
+--><GeneratorsGroup><Generator name="active_record:migration" /><Generator name="active_record:model" /><Generator name="active_record:observer" /><Generator name="active_record:session_migration" /><Generator name="assets" /><Generator name="backbone:install" /><Generator name="backbone:model" /><Generator name="backbone:router" /><Generator name="backbone:scaffold" /><Generator name="coffee:assets" /><Generator name="controller" /><Generator name="erb:controller" /><Generator name="erb:mailer" /><Generator name="erb:scaffold" /><Generator name="factory_girl:model" /><Generator name="generator" /><Generator name="helper" /><Generator name="integration_test" /><Generator name="jquery:install" /><Generator name="js:assets" /><Generator name="mailer" /><Generator name="migration" /><Generator name="model" /><Generator name="mongoid:config" /><Generator name="observer" /><Generator name="performance_test" /><Generator name="resource" /><Generator name="rspec:controller" /><Generator name="rspec:helper" /><Generator name="rspec:install" /><Generator name="rspec:integration" /><Generator name="rspec:mailer" /><Generator name="rspec:model" /><Generator name="rspec:observer" /><Generator name="rspec:scaffold" /><Generator name="rspec:view" /><Generator name="scaffold" /><Generator name="scaffold_controller" /><Generator name="session_migration" /><Generator name="task" /><Generator name="websocket_rails:install" /></GeneratorsGroup></Settings>
... ...
@@ -17,6 +17,8 @@ group :assets do
17 17
   gem 'coffee-rails', '~> 3.2.1'
18 18
   gem 'normalize-rails'
19 19
   gem 'compass-rails'
20
+  gem "rails-backbone"
21
+  #gem "marionette-rails", "~> 0.10.2"
20 22
 
21 23
   # See https://github.com/sstephenson/execjs#readme for more supported runtimes
22 24
   # gem 'therubyracer', :platforms => :ruby
... ...
@@ -63,6 +63,7 @@ GEM
63 63
       compass (>= 0.12.2, < 0.14)
64 64
     daemons (1.1.9)
65 65
     diff-lcs (1.1.3)
66
+    ejs (1.0.0)
66 67
     em-synchrony (1.0.3)
67 68
       eventmachine (>= 1.0.0.beta.1)
68 69
     erubis (2.7.0)
... ...
@@ -143,6 +144,10 @@ GEM
143 143
       activesupport (= 3.2.12)
144 144
       bundler (~> 1.0)
145 145
       railties (= 3.2.12)
146
+    rails-backbone (0.7.2)
147
+      coffee-script (~> 2.2.0)
148
+      ejs (~> 1.0.0)
149
+      railties (>= 3.1.0)
146 150
     railties (3.2.12)
147 151
       actionpack (= 3.2.12)
148 152
       activesupport (= 3.2.12)
... ...
@@ -227,6 +232,7 @@ DEPENDENCIES
227 227
   mongoid (~> 3.0.20)
228 228
   normalize-rails
229 229
   rails (~> 3.2.11)
230
+  rails-backbone
230 231
   rspec-rails
231 232
   sass-rails (~> 3.2.3)
232 233
   test-unit
233 234
deleted file mode 100755
... ...
@@ -1,6 +0,0 @@
1
-this.App =
2
-  UI:
3
-    setAutoHeight: ->
4
-      height = $(window).height()
5
-      $("#height-setter-1").css({height: height - 50})
6
-      $("#height-setter-2").css({height: height - $('#tabbar').outerHeight() - $('#msg-writer').outerHeight() - 57})
7 1
new file mode 100755
... ...
@@ -0,0 +1,13 @@
0
+this.App =
1
+  UI:
2
+    setAutoHeight: ->
3
+      height = $(window).height()
4
+      $("#height-setter-1").css({height: height - 50})
5
+      $("#height-setter-2").css({height: height - $('#tabbar').outerHeight() - $('#msg-writer').outerHeight() - 57})
6
+
7
+  Com:
8
+    connect: ->
9
+      App._dispatcher = new WebSocketRails('localhost:3000/websocket')
10
+
11
+    fetch: (options) ->
12
+      App._dispatcher.trigger(options.event, options.data, options.success, options.error)
0 13
deleted file mode 100755
... ...
@@ -1,18 +0,0 @@
1
-// This is a manifest file that'll be compiled into application.js, which will include all the files
2
-// listed below.
3
-//
4
-// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
-// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
-//
7
-// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
-// the compiled file.
9
-//
10
-// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
-// GO AFTER THE REQUIRES BELOW.
12
-//
13
-//= require jquery
14
-//= require jquery_ujs
15
-//= require _main
16
-//= require_tree .
17
-//= require websocket_rails/main
18
-
19 1
new file mode 100755
... ...
@@ -0,0 +1,5 @@
0
+#= require jquery
1
+#= require jquery_ujs
2
+#= require _main
3
+#= require chat
4
+
0 5
new file mode 100644
... ...
@@ -0,0 +1,8 @@
0
+#= require websocket_rails/main
1
+#= require underscore
2
+#= require backbone
3
+#= require backbone_rails_sync
4
+#= require backbone_datalink
5
+#= require backbone/xmpp
6
+
7
+App.Com.connect()
0 8
new file mode 100644
1 9
new file mode 100644
... ...
@@ -0,0 +1,17 @@
0
+class Xmpp.Models.Contact extends Xmpp.Models.Model
1
+  namespace: 'app.roster'
2
+
3
+  defaults:
4
+    name: 'unnamed'
5
+    age: 0
6
+
7
+  initialize: ->
8
+    console.log "Welcome stranger"
9
+
10
+    @on("change:name", (model) ->
11
+      name = model.get("name");
12
+      console.log "Changed my name to " + name
13
+    )
14
+
15
+class Xmpp.Collections.ContactsCollection extends Backbone.Collection
16
+  model: Xmpp.Models.Contact
0 17
new file mode 100644
... ...
@@ -0,0 +1,33 @@
0
+class Xmpp.Models.Model extends Backbone.Model
1
+  communicator: App.Com
2
+
3
+  sync: (method, model, options) ->
4
+    params = {}
5
+
6
+    if (!options.event)
7
+      params.event = model.namespace + '.' + method
8
+
9
+    if (!options.data? && model && (method == 'create' || method == 'update' || method == 'patch'))
10
+      params.data = JSON.stringify(options.attrs || model.toJSON(options))
11
+    else
12
+      params.data = JSON.stringify({})
13
+
14
+    success = options.success;
15
+    success = (resp) ->
16
+      if (success)
17
+        success(model, resp, options)
18
+      model.trigger('sync', model, resp, options)
19
+
20
+    error = options.error
21
+    options.error = (xhr) ->
22
+      if (error)
23
+        error(model, xhr, options)
24
+      model.trigger('error', model, xhr, options);
25
+
26
+    if (! @communicator?)
27
+      throw new Error('A communicator object must be specified');
28
+
29
+    result = options.result = @communicator.fetch(_.extend(params, options))
30
+
31
+    model.trigger('request', model, result, options)
32
+    return result
0 33
\ No newline at end of file
1 34
new file mode 100644
2 35
new file mode 100644
3 36
new file mode 100644
4 37
new file mode 100644
... ...
@@ -0,0 +1,12 @@
0
+#= require_self
1
+#= require_tree ./templates
2
+#= require      ./models/model
3
+#= require_tree ./models
4
+#= require_tree ./views
5
+#= require_tree ./routers
6
+
7
+window.Xmpp =
8
+  Models: {}
9
+  Collections: {}
10
+  Routers: {}
11
+  Views: {}
0 12
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+# Place all the behaviors and hooks related to the matching controller here.
1
+# All this logic will automatically be available in application.js.
2
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
... ...
@@ -7,7 +7,7 @@ class ApplicationController < ActionController::Base
7 7
 			create_new_authentification()
8 8
 
9 9
             unless controller_name == 'chat'
10
-                redirect_to chat_path
10
+                redirect_to chat_template_path
11 11
             end
12 12
 		else
13 13
 			unless controller_name == 'sessions' && (action_name == 'new' || action_name == 'create')
... ...
@@ -2,5 +2,9 @@ class ChatController < ApplicationController
2 2
 
3 3
 	def index
4 4
 
5
-	end
5
+    end
6
+
7
+    def ws
8
+
9
+    end
6 10
 end
7 11
new file mode 100644
... ...
@@ -0,0 +1,21 @@
0
+class RosterController < WebsocketRails::BaseController
1
+
2
+    def initialize_session
3
+        controller_store[:friends] = {}
4
+    end
5
+
6
+    ##
7
+    # Vrat vsetkych priatelov v rosteri
8
+    def get
9
+        #logger.debug request.
10
+        #controller_store[:friends]
11
+
12
+        trigger_success message: "yes"
13
+    end
14
+
15
+    ##
16
+    # Pridaj noveho priatela do zoznamu
17
+    def add_friend(data)
18
+
19
+    end
20
+end
... ...
@@ -14,7 +14,7 @@ class SessionsController < ApplicationController
14 14
 		end
15 15
 
16 16
         create_new_authentification()
17
-        redirect_to chat_path, flash: {notice: I18n.t('login.success')}
17
+        redirect_to chat_template_path, flash: {notice: I18n.t('login.success')}
18 18
 	end
19 19
 
20 20
 	def destroy
21 21
new file mode 100644
... ...
@@ -0,0 +1,2 @@
0
+module RosterHelper
1
+end
0 2
new file mode 100644
... ...
@@ -0,0 +1,5 @@
0
+- content_for :title, t("chat.title")
1
+- content_for :javascript, javascript_include_tag("application")
2
+- content_for :javascript, javascript_include_tag("backbone-ws")
3
+
4
+#content
... ...
@@ -28,4 +28,6 @@ Xmpp::Application.configure do
28 28
 
29 29
   # Expands the lines which load the assets
30 30
   config.assets.debug = true
31
+
32
+  config.middleware.delete Rack::Lock
31 33
 end
... ...
@@ -41,4 +41,12 @@ WebsocketRails::EventMap.describe do
41 41
   #     subscribe :new, :to => ProductController, :with_method => :new_product
42 42
   #   end
43 43
   # The above will handle an event triggered on the client like `product.new`.
44
+
45
+    namespace :app do
46
+
47
+        namespace :roster do
48
+            subscribe :read, :to => RosterController, :with_method => :get
49
+        end
50
+
51
+    end
44 52
 end
... ...
@@ -8,5 +8,6 @@ Xmpp::Application.routes.draw do
8 8
     match '/signin',  to: 'sessions#new'
9 9
     match '/signout', to: 'sessions#destroy', via: :delete
10 10
 
11
-    match '/chat', to: 'chat#index'
11
+    match '/ws', to: 'chat#ws'
12
+    match '/chat_template', to: 'chat#index'
12 13
 end
13 14
new file mode 100644
... ...
@@ -0,0 +1,7 @@
0
+require 'test_helper'
1
+
2
+class RosterControllerTest < ActionController::TestCase
3
+  # test "the truth" do
4
+  #   assert true
5
+  # end
6
+end
0 7
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+require 'test_helper'
1
+
2
+class RosterHelperTest < ActionView::TestCase
3
+end