我使用的是Underscore模板.可以附加外部文件作为模板吗?
在Backbone View中我有:
textTemplate: _.template( $('#practice-text-template').html() ),
initialize: function(){
this.words = new WordList;
this.index = 0;
this.render();
},
Run Code Online (Sandbox Code Playgroud)
在我的HTML中是:
<script id="practice-text-template" type="text/template">
<h3>something code</h3>
</script>
Run Code Online (Sandbox Code Playgroud)
它运作良好.但我需要外部模板.我尝试:
<script id="practice-text-template" type="text/template" src="templates/tmp.js">
Run Code Online (Sandbox Code Playgroud)
要么
textTemplate: _.template( $('#practice-text-template').load('templates/tmp.js') ),
Run Code Online (Sandbox Code Playgroud)
要么
$('#practice-text-template').load('templates/tmp.js', function(data){ this.textTemplate = _.template( data ) })
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我在关联类中绑定布尔属性时遇到问题.如果我选中复选框(好),则属性设置为true,但如果未选中checbox ,则属性为null.
我知道HTML复选框的问题.我知道为什么在params中发送"_fieldName",但是这个"_fieldName"不会将我的boolean属性设置为false.
class Person{
String title
List<Group> groups = new ArrayList()
static hasMany = [groups: Groups]
}
class Group{
String title
Boolean isHidden
static belongTo = Person
}
class PersonController{
def form = {
def person = new Person()
person.groups.add( new Group() )
return ["person": person]
}
def handleForm = {
def person = new Person( params )
println person.groups[0]
}
}
<g:form action="save">
<g:textField name="title" value="${person?.title}" />
<g:textField name="groups[0].title" value="${person?.groups[0]?.title}"/>
<g:checkBox name="groups[0].isHidden" …Run Code Online (Sandbox Code Playgroud) 如何确定集合中的Object类型?
class Human{...}
class Man extends Human{...}
class Women extends Human{...}
def humans = Human.findAll()
humans.each(){ human ->
// ??? , it is not work
if ( human instanceof Man ) { println "Man" }
if ( human instanceof Woman ) { println "Woman" }
}
Run Code Online (Sandbox Code Playgroud)
非常感谢,
汤姆
如何跳转到控制器中的另一个动作?
我有表单和几个提交按钮.每个submmit按钮都有名称.
<g:form action="save" method="post">
<g:input name="title" value="${letter.title}" />
<g:input name="comments[0].text" value="${letter.comments[0].text}" />
<g:submitButton name="save" value="save" />
<g:submitButton name="addComment" value="add" />
</g:form>
def save = {
if (params.addComment){
letter.addToComents( new Comment() )
render(view:'form', model:["letter": letter])
return
}
...
if ( letter.save() )
...
}
def addComment = {
...
}
Run Code Online (Sandbox Code Playgroud)
它有效,但并不好.我想将块"addComment"中的代码移动到动作addComment:
def save = {
if (params.addComment){
// it don´t work
redirect ( action:"addComment" )
}
...
if ( letter.save() )
...
}
def addComment = {
letter.addToComents( new Comment() …Run Code Online (Sandbox Code Playgroud) 我读过这篇文章:https: //www.ibm.com/developerworks/java/library/j-javadev2-8/index.html
清单2中的抽象类Model具有静态变量数据存储区.
abstract class Model {
static def datastore = DatastoreServiceFactory.datastoreService
...
Run Code Online (Sandbox Code Playgroud)
清单3中的Race类扩展了抽象类Model.
class Race extends Model {
public Race(params){
super(params)
}
}
Run Code Online (Sandbox Code Playgroud)
在清单5和清单6 中,使用静态方法中的作者非静态变量数据存储(this.datastore).我想,静态方法是在Race类中.
static def findByName(name){
def query = new Query(Race.class.simpleName)
query.addFilter("name", Query.FilterOperator.EQUAL, name)
def preparedQuery = this.datastore.prepare(query)
if(preparedQuery.countEntities() > 1){
return new Race(preparedQuery.asList(withLimit(1))[0])
}else{
return new Race(preparedQuery.asSingleEntity())
}
}
Run Code Online (Sandbox Code Playgroud)
这怎么可能?谢谢你的解释.
汤姆
嗨(这是一个非常愚蠢的问题)
如何在测试中从数据库中获取Domain类?
class PollServiceTests extends GrailsUnitTestCase {
Integer id = 1
void testSomething() {
Teacher teacher1 = Teacher.get(id)
assert teacher1 != null
}
}
Run Code Online (Sandbox Code Playgroud)
我总是得到null或
没有方法签名:cz.jak.Teacher.get()适用于参数类型:(java.lang.Integer)值:[1]
非常感谢汤姆