2016-04-09 38 views
0

我正在學習過去兩週的角色。我認爲使用前端框架非常有趣,我沒有想到我將不得不創建2個單獨的完整應用:)。我已經將你一些問題Angular + Rails初學者的問題

  1. 什麼是管理角度像鐵軌user.animals.where模型連接(...)

例如最好的方法 我有用戶模型,每個用戶有很多動物。 在頁面上,我想顯示 與用戶動物和所有動物的右表的鏈接表, 現在我向動物控制器給用戶和動物一次兩個承諾,它是查詢數量的兩倍,很難管理時我想添加一個表單來創建一個新的動物,因爲我必須將新對象推入兩個角型陣列中

此外,當每個動物可以擁有很多玩具並且用戶只能添加一個玩具時,真的很複雜

我必須從軌道控制器發送json幷包含動物玩具,並且還包含用戶id的數組,然後在視圖

ng-hide="(animal['users_ids'].includes(user.id)" 

有了這個解決方案,當我創建一個新的模型時,我必須更新玩具模型和動物,它可以做得更簡單嗎?

  1. 當角度和軌道模型發生變化時,同步角度和軌道模型的最佳方法是什麼?

我可以每隔x秒刷新頁面,但它不是親。

  1. 我已經確認了rails和angular與devise和oauth一起工作。我的配置工作,但我不知道如果解決方案是牢固的,安全的

    Gemfile 
    gem 'devise' 
    gem 'omniauth-facebook' 
    gem 'omniauth-github' 
    gem 'angular_rails_csrf' 
    

軌產生色器件:安裝

建立在現有的用戶模型

devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook, :github] 

    def self.from_omniauth(auth) 
     where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
     user.provider = auth.provider 
     user.uid = auth.uid 
     user.email = auth.info.email 
     user.password = Devise.friendly_token[0,20] 
     end 
    end 

設立色器件初始化器

config.clean_up_csrf_token_on_authentication = true 

個設置onniauth初始化

Rails.application.config.middleware.use OmniAuth::Builder do 
     provider :facebook, ENV['APP_ID'], ENV['APP_SECRET'], 
     scope: 'email,public_profile', info_fields: 'email,id,name' 
    end 

建立路由

devise_for :users, :controllers => { :omniauth_callbacks => "sessions#create" } 
    get '/auth/:provider/callback', to: 'sessions#create' 

會話控制器

 def facebook 
     @account = User.from_omniauth(request.env["omniauth.auth"]) 
     sign_in @account 
     render 'sessions/create', layout: false 
     end 

     def github 
     @account = User.from_omniauth(request.env["omniauth.auth"]) 
     sign_in @account 
     render 'sessions/create', layout: false 
     end 

應用/視圖/會話/ create.html上。ERB

<p>This view will now self-destruct</p> 
    <script> 
     try { 
      window.opener.$windowScope.handlePopupAuthentication('<%= @provider %>', <%= @account.to_json.html_safe %>); 
     } catch(err) {} 
     window.close(); 
    </script> 

和角度配置

應用程序/資產/模板/ login.html.slim

div.page-header 
     h1 Log In 

    div class="btn dash-subs login-btn" ng-click="authNetwork('facebook')" 
     p Login With Facebook 

應用程序/資產/ JavaScript的/ AUTH/authCtrl.js

angular.module('myAPP') 
     .controller('AuthCtrl', [ 
     '$scope', 
     '$state', 
     'Auth', 
     function($scope, $state, Auth){ 

     $scope.handlePopupAuthentication = function handlePopupAuthentication(network, account) { 
      $scope.$apply(function(){ 
      window.location.reload() 
      }); 
     } 

     $scope.authNetwork = function authNetwork(network) { 
      var openUrl = '/users/auth/' + network 
      window.$windowScope = $scope; 
      window.open(openUrl, "Authenticate Account", "width=500, height=500"); 
     }; 
    }]) 

回答

0

在角度上查找ng-change指令,只要輸入發生變化,您就可以觸發任何功能要

您可以找到文檔here,並有多個實例別的地方太

此外,您還可以避免兩個請求一個爲用戶和其他動物,它可以通過在軌使用主動模式串行避免,這將真正幫助

這裏是串行一個不錯的視頻,railscasts

+0

刷新頁面始終處於Angularjs可以避免的,如果你去通過此路徑確保始終有一個更好的解決方案 – amrdruid