如何使Ember.js与Grails控制器名称一致? [英] How to Make Ember.js Behave with Grails Controller Names?

查看:82
本文介绍了如何使Ember.js与Grails控制器名称一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails非常强大,您可以使用单个语句( 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆