添加需求:['ApplicationController']使我的视图消失

时间:2014-03-09 00:03:57

标签: ember.js ember-model

我的Ember.js应用程序设置大致如下:

路由器:

App.Router.map ->
  @resource('site', { path: '/' }, ->
    @resource('dashboard')
    @resource('account')
    @resource('pages', { path: '/:page_slug'}))

路线:

App.ApplicationRoute = Ember.Route.extend
    model: ->
        return App.Site.find(1)

App.PagesRoute = Ember.Route.extend
    model: (params)->
        return App.Page.find(params.page_slug)

编辑:

控制器(JS不是咖啡):

App.PagesController = Ember.ObjectController.extend({
    needs: 'ApplicationController',
    ...
});

我有一个ApplicationController和一个PagesController。当我在页面上时,我想调用一个动作来删除当前页面。如果我将它放在PagesController中它可以正常工作,但在刷新页面之前,ApplicationView中带有页面列表的导航菜单不会更新。所以...我假设我需要将操作放在ApplicationController中并将needs: ['ApplicationController']添加到我的PagesController。

然而,当我这样做时,我的页面模板中的所有内容都会消失。

我的应用程序模板中有{{outlet}},而网站模板中有另一个,最终显示页面模板。是的,复杂的,我知道并且可能有更好的方法,所以任何建议都将非常感激。哦,顺便说一句,我是一个真正的Ember.js newb,所以需要明确说明示例。 (用蜡笔画出漂亮的颜色实际上是理想的。)

提前感谢您的帮助。

这是刷新问题。在根资源site中,我加载了一个Site模型,Rails后端使用发送的url返回。最终,此应用将与多个域一起使用,并且每个域都将被提供它是自己的网站(大致基于WP-Multisite概念)。然后在pages路由中,我根据其slug属性加载网站的一个页面。这一切都很好。所以,现在我想让用户能够随意添加和删除页面。

所以,问题是这个。当我删除PagesController级别的页面时,页面删除得很好,但ApplicationController不会收到通知。即,我的导航菜单:

<ul class="left">
  {{#each page in page}}
    <li>{{#link-to 'pages' page.slug}}{{page.menu_name}}{{/link-to}}</li>
  {{/each}}
    <li id="add-page-button"><a data-tooltip title="Click here to add a new page to your site." href="#" data-reveal-id="addPageModal" data-reveal>+</a></li>
</ul>

没有得到关于页面丢失的通知,因此它不会通过从列表中删除该页面来更新导航菜单。添加工作正常,添加新页面时导航列表会更新,但我在ApplicationController级别这样做:

addNewPage: ->
            # Get the site
            site = @get('model')
            # Get all the pages associated with the site
            pages = site.get('page')

            title = this.get('newTitle')
            if (!title.trim())
                return

            slug = this.get('newSlug')
            if (!slug.trim())
                return

            menu_name = this.get('newMenuName')
            if (!menu_name.trim())
                return

            # Create a new page passing in our title, slug and menu_name
            pages.create({title: title, slug: slug, menu_name: menu_name})
            # Save the pages
            pages.save()

            if (pages.isError)
                console.log(pages.errors)
            else
                @set('newTitle', '')
                @set('newSlug', '')
                @set('newMenuName', '')
                $('#addPageModal').foundation('reveal', 'close')

这是deletePage上的PagesController代码(对不起,我有JS和CoffeeScript的混合代码):

deletePage: function (slug) {           
    var page = this.get('model');
    this.get('ApplicationController').deletePage(page);
    this.toggleProperty('isEditingTitle');
    this.toggleProperty('isShowingDeleteConfirmation');
    this.transitionToRoute('site.index');
}

当我尝试这个时,Ember相当有帮助地让我知道我需要添加needs但是当我这样做时,我的页面模板是空白的。

哦,是的,我甚至尝试在调用target操作的按钮上设置deletePage属性。

我意识到我需要做的是以某种方式访问​​Site中的PagesController模型,但我应该怎么做。

这篇文章占据了史诗般的比例。对于那个很抱歉。再次感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

需求在路线上没有任何作用,如果您想从路线访问控制器,您可以使用this.controllerFor('application')

此外,在控制器定义中使用需求时,它应该是needs: 'application'needs: ['foo', 'application'] ...

答案 1 :(得分:0)

好的,我发现了。为了后人的缘故(以及其他寻求此事的人)。以下是如何删除具有belongsTo关联的模型中的对象。

的ApplicationController:

deletePage: function () {
  # First we find the Site which all pages belongTo
  # Just ignore the hard-coded '1' it's something I have to do to get it to work
  site = App.Site.find(1);

  # Then we get the current model, i.e., the 'page' we're on and about to delete
  page = this.get('model');

  # Then, we get all 'pages'
  pages = site.get('page');

  # We remove the object from the page which updates the navigation menu
  pages.removeObject(page);

  # We delete the record and save the 'pages' model
  page.deleteRecord();
  pages.save();
}

页面模板:

{{#if isShowingDeleteConfirmation}}
<div class="large-7 large-offset-5">
  <label class="inline left">Are you sure? (Can't be undone)</label>
  <button {{action 'deletePage'}} class='button tiny alert'>Yes</button>
  <button {{action 'cancelDeletePage'}} class='button tiny'>Cancel</button>
</div>
{{else}}
<div class="large-2 right">
  <button {{action 'showDeleteConfirmation'}} class='button tiny alert'>Delete Page</button>
</div>
{{/if}}

如果你需要我,我可以告诉你if ... else语句背后的属性和逻辑。

基本上,用户点击“删除”按钮,显示其他按钮“是”和“取消”。单击“是”后,将调用deletePage操作。

我发现很少有关于deleteRecord方法的文档,基本上我必须把它们拼凑在一起。也许有更好的方法,这可以重构。请在评论中告诉我。