2013-10-24 122 views
1

我有一个灰烬应用程序,它有一个profiles路径来显示数据库中特定项目的标题和描述。我想为该数据创建一个edit页面,如在ember中的CRUD。我在profiles资源路由下实施了edit路由。灰烬编辑模板

index模板工作正常,它成功地填充数据库中的数据,结果显示在模板中。但是,只要我为该路由声明一个控制器并向该控制器添加一个操作,该模板就会中断。

这是应用程序的app.js代码。

App.Profile = DS.Model.extend({ 
title: DS.attr('string'), 
description: DS.attr('string') 
}); 

App.Router.map(function(){ 
this.resource("about", function(){ 
    this.route("create", {path: '/create'}); 
}); 
this.resource('profiles', {path: '/profiles/:profiles_id'}, function(){ 
    this.route('edit'); 
}); 
this.resource('login'); 
this.resource("logout"); 
}); 

App.ProfilesController = Ember.Controller.extend(); 

App.ProfilesRoute = Ember.Route.extend({ 
model: function(params) { 
    NProgress.start(); 
    var data = App.Profile.find(params.profiles_id); 
    data.then(function(){ 
     NProgress.done(); 
    }); 
    return data; 
} 
}); 

的HTML代码的配置文件:

<script type="text/x-handlebars" data-template-name="profiles"> 
    <div class="container"> 
    <div class="row"> 
     <h1>{{title}}</h1> 
    <p> 
    {{description}} 
    </p> 
    <form {{action edit on="submit"}}> 
    {{input value=title type="text" placeholder="Title"}} 
    {{textarea value=description}} 
    {{input class="button" type="submit" value="Edit"}} 
    </form> 
</div> 
    </div> 
</script> 

此时一切正常。但只要我将编辑动作添加到ProfileController,当我在浏览器中加载页面时,只显示没有数据的表单。什么地方出了错?

当我将此代码添加到ProfilesController磺酰基,没有数据的形式显示:

App.ProfilesController = Ember.Controller.extend({ 
    edit: function(){ 
     var self =this, data = this.getProperties('title', 'description'); 
     self.set('errorMessage', null); 
     self.set('successMsg', null); 
     Ember.$.put('profiles', data, function(){ 
      NProgress.start(); 
     }).then(function(response){ 
      NProgress.done(); 
      if(response.success){ 

      }else { 
       self.set('errorMessage', response.message); 
       console.log(response.message); 
      } 
     }); 
    } 
}); 

我想创建一个动作,当表单提交,编辑操作被触发并做穿上服务器,我有php laravel 4后端在服务器来处理请求RESTfully。

我试图在配置文件/编辑模板中的编辑模板下实现上面的代码,但我无法使它工作,所以我将代码粘贴到配置文件的索引模板中。

灰烬显示该日志在控制台上产生

- >路线:profiles.index对象{全名: “路线:profiles.index”}烬-1.0.0.js:356

渲染应用与默认视图<( Ember.View的子类):ember330>对象{全名: “视图:应用”} 烬-1.0.0.js:356个

与默认视图 对象{全名渲染配置文件: “视图:轮廓”}烬-1.0.0.js:356

产生 - >控制器:profiles.index对象{全名: “控制器:profiles.index”}烬-1.0.0.js:356

找不到“profiles.index”模板或视图。没有什么会是 渲染对象{全名: “模板:profiles.index”} 烬-1.0.0.js:356

产生 - >路线:索引对象{全名: “路线:索引”} ember- 1.0.0.js:356

generated - > route:about.index Object {fullName:“route:about.index”} ember-1.0.0。JS:356

回答

0

几件事情:

  1. 为了把它里面的配置文件/编辑模板,你需要拥有{{口}}定义的profiles.hbs模板。这样编辑模板就会显示出来。另外,profiles.hbs应该位于profiles目录之外。和profiles目录应该包含索引和编辑模板。

  2. 我认为它应该是一个ArrayController而不是Controller。

我认为一旦你根据第1点改变,它应该正常工作。试一试。

+0

谢谢你的回答,但到现在为止我还没有使用hbs文件,我不知道如何在后端使用laravel,使用上面的代码,请你稍微描述一下,控制器和hbs更正,你是建议? – monk

+0

您不必使用hbs文件。基本上配置文件data-template-name应该有{{outlet}},并且您将拥有另外两个带有配置文件/索引和配置文件/编辑的数据模板名称。您也可以将它们全部保存在单个文件中。 – VNarasimhaM