如何使Ember.js与Grails控制器名称一致? [英] How to Make Ember.js Behave with Grails Controller Names?
问题描述
object as JSON
)将您的Domain对象转换为JSON。不幸的是,这不足以与Ember.js进行交互,原因有几个。 如何使Grails与Ember.js一起玩得很好?
好的问题,自我!
Ember.js及其自动AJAX -browser-data-store ninjitsu希望URL看起来有一定的方式,而JSON看起来是一定的。由于每个工具的工作原理,更容易做出符合 两个方面的更改!
Grails提供的
对于域类 MyCoolDomainClass
与控制器 MyCoolDomainClassController
,Grails想提供URL / app / myCoolDomainClass
。如果您将控制器设置为包含以下内容:
def index(){render MyCoolDomainClass.list()as JSON}
你会得到一个如下回应:
[{id:1,name:Bob},{id:2,name:Sally}]
Ember想要的
在Ember.js中,您可以创建一个具有相同属性的模型。通过利用 ember-data
,您可以轻松地使用后端在浏览器中连接数据存储。不幸的是,Ember.js想要的是不同的。它希望url / my_cool_domain_class
提供数据:
{mycooldomainclass:[ {id:1,name:Bob},{id:2,nameSally}]}
协调
更新:我创建了一个 ember-data-grails
repo on Github ,它会为您处理所有这些修改,并演示如何使控制器能够很好地播放!
Grails is pretty powerful, and lets you turn your Domain objects into JSON with a single statement (object as JSON
). Unfortunately, this is not sufficient to interact with Ember.js for a few reasons.
How can I make Grails play nicely with Ember.js?
Good question, self!
Ember.js, and its auto-AJAX-in-browser-data-store ninjitsu expect the URL to look a certain way, and the JSON to look a certain way. Due to the way that each tool works, it's easier to make conforming changes to both sides of the equation!
What Grails Provides
For a Domain class MyCoolDomainClass
with a controller MyCoolDomainClassController
, Grails wants to provide the URL /app/myCoolDomainClass
. If you set up the controller to contain something along the lines of:
def index() { render MyCoolDomainClass.list() as JSON }
You'll get a response that looks like:
[{ id: 1, name: "Bob"}, {id: 2, name: "Sally"}]
What Ember Wants
In Ember.js, you might create a Model with the same properties. By leveraging ember-data
, you can easily hook up the Data Store in the browser with your backend. Unfortunately, what Ember.js wants is different. It expects the the url /my_cool_domain_class
to provide the data:
{ mycooldomainclass: [ { id: 1, name: "Bob"}, {id: 2, name "Sally"}] }
Reconciling
UPDATE: I've created a ember-data-grails
repo on Github which takes care of all of these modifications for you, and demonstrates how to make a controller that plays along nicely!
这篇关于如何使Ember.js与Grails控制器名称一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!