Ruby on rails 3.2 ember.js registerBoundHelper:创建一个类似if的帮助程序,通过json执行服务器端授权
javascript/coffeescript/ember.js/stackoverflow新手在这里 我正在尝试创建一个if-like帮助程序,该帮助程序仅在对象上的操作获得服务器授权时才显示内容。基本上我想要rails cancan gem中的Ruby on rails 3.2 ember.js registerBoundHelper:创建一个类似if的帮助程序,通过json执行服务器端授权,ruby-on-rails-3.2,ember.js,cancan,handlebars.js,ember-data,Ruby On Rails 3.2,Ember.js,Cancan,Handlebars.js,Ember Data,javascript/coffeescript/ember.js/stackoverflow新手在这里 我正在尝试创建一个if-like帮助程序,该帮助程序仅在对象上的操作获得服务器授权时才显示内容。基本上我想要rails cancan gem中的can?方法。我想写这样的模板: <h1>Viewing profile for {{email}}</h1> <h2>Scores</h2> {{#each score in scores}}
can?
方法。我想写这样的模板:
<h1>Viewing profile for {{email}}</h1>
<h2>Scores</h2>
{{#each score in scores}}
<div class="summary">{{score.what}}
{{#can score action=destroy}}
<a href="#" id="destroy">Destroyable</a>
{{/can}}
{{#can score action=view}}
<a href="#" id="view">Viewable</a>
{{/can}}
{{#can score action=edit}}
<a href="#" id="edit">Editable</a>
{{/can}}
</div>
{{/each}}
<site>/api/v1/authorization.json?action=destroy&cName=Score&id=1
scope "/api" do
scope "/v1" do
resource :authorization, only: [:show]
end
end
服务器将简单地返回HTTP状态代码200或401,而ember将根据状态代码神奇地显示或隐藏内容。我必须以这种方式进行授权,因为服务器上必须进行一些基于角色和对象的权限检查,我不想在js中复制授权逻辑
到目前为止,我已经使用现成的if
helper作为示例来创建以下自定义绑定的helper(coffeescript)
以下是用户对象:
EmberDeviseExample.User = DS.Model.extend
email: DS.attr('string')
scores: DS.hasMany('EmberDeviseExample.Score')
EmberDeviseExample.store.adapter.serializer.map('EmberDeviseExample.User', {scores: {embedded: 'load'}})
下面是授权对象:
EmberDeviseExample.Authorization = Ember.Object.extend
action: ''
object: null
response: 401
urlBase: ApiUrl.authorization_path
can : (->
return (this.get('response') == 200)
).property('response')
authorize: ->
# if object is an instance, include the id in the query params
# otherwise, just include the class name
obj = this.get('object')
cName = obj.toString()
id = null
if Ember.typeOf(obj) == "instance"
# cname looks something like "<namespace.name:embernumber>"
# turn it into "name"
cName = cName.split(':')[0].split('.')[1]
id = obj.get('id')
$.ajax
url : "#{this.get('urlBase')}.json"
context : this
type : 'GET'
data :
action : this.get('action')
cName : cName
id : id
complete : (data, textStatus, xhr) ->
this.set('response', data.status)
return this.get('can')
当然,我的应用程序中还有其他代码,如果需要查看,请告诉我
当我删除#can
帮助程序时,我会看到渲染的模板。当我添加#can
助手时,我得到一个javascript错误,Chrome中的消息是“uncaughttypeerror:cannotreadproperty'0'of null”@ember.prod.js:2362。
有人知道如何构建一个接受对象和操作、运行服务器端授权检查并根据服务器结果显示/隐藏内容的帮助器吗?刚刚回答了您的问题,有点晚了,但我可能仍然可以提供帮助
虽然要知道错误可能来自何处很复杂,但我在预期的实现中看到了一个问题:您正在将自定义if帮助程序绑定到属性“score”,而观察响应中的更改的属性称为“can”,因为帮助程序。。。(但这一条不会被帮助者看到,也不会被绑定!)顺便说一句,如果有更好的方法来隐藏内容,请发布它!我很感激你的努力,但在这个阶段,我真的不知道这是不是正确的答案。我想我找到了一些解决这个问题的方法,然后不久就决定放弃它,等待ember.js成熟。
class AuthorizationsController < ApplicationController
respond_to :json
def show
id = params[:id]
cName = params[:cName]
action = params[:action]
object = cName.capitalize.constantize
object = object.find(id) unless id.blank?
authorized = can? action, object
render status: (authorized ? 200 : 401), json: {}
end
end
scope "/api" do
scope "/v1" do
resource :authorization, only: [:show]
end
end