1

javascript/coffeescript/ember.js/stackoverflow 新手在这里!

我正在尝试创建一个类似 if 的助手,它仅在服务器授权对象上的操作时才显示内容。基本上我想要can?来自 rails cancan gem 的方法。我想写这样的模板:

<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}}

canember.js 助手将使用如下所示的 json 请求查询服务器:

<site>/api/v1/authorization.json?action=destroy&cName=Score&id=1

服务器会简单地返回 HTTP 状态码 200 或 401,而 ember 会根据状态码神奇地显示或隐藏内容。我必须以这种方式进行授权,因为必须在服务器上进行一些基于角色和对象的权限检查,并且我不想在 js 中复制授权逻辑。

到目前为止,我已经使用开箱即用的if帮助器作为示例来创建以下自定义绑定帮助器(coffeescript)。

Ember.Handlebars.registerBoundHelper 'can', (object, options) ->

    permission = EmberDeviseExample.Authorization.create
        action: options.hash.action
        object: object

    permission.authorize()

    options.contexts = [permission]

    Ember.Handlebars.helpers.boundIf.call(permission, "can", options)

这是用户对象:

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')

在服务器端,authorization.json 代码如下所示(rails 3)。我正在使用 cancan gem 来控制授权:

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

当然,我的应用程序中还有其他代码,如果您需要查看,请告诉我。

当我删除#can助手时,我看到了渲染的模板。当我重新添加#can助手时,我收到一个 javascript 错误,Chrome 中的消息是"Uncaught TypeError: Cannot read property '0' of null" @ ember.prod.js:2362.

有谁知道如何构建一个接受对象和操作、运行服务器端授权检查并根据服务器结果显示/隐藏内容的助手?

4

1 回答 1

0

刚刚遇到你的问题,有点晚了,但可能我仍然可以提供帮助。

尽管查看错误的来源很复杂,但我在您的预期实现中发现了一个问题:您将自定义 if 助手绑定到属性“score”,而观察响应变化的属性称为“can” ,作为助手......(但是从助手那里没有观察到这个,也没有被约束!)

于 2015-03-20T15:15:12.307 回答