本文介绍了灰烬选择发生变化,但我的模型仍然“不脏”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,MyModel和MyOptions。

I have two models, MyModel and MyOptions.

MyModel有一个myValue属性归属于('myOption)和myName('string')。

MyModel has a myValue property belongsTo('myOption), and myName('string').

在视图中,我有一个myName输入和一个选择,其中包含模型MyOptions的可能值。

In the view, I have an input for myName and a select with possible values of the model MyOptions.

当我选择一个新的相关行,我希望myModel是脏的。如果我更改了myName,myModel(正确)会变脏。

When I select a new related row, I expect myModel to be 'dirty'. If I change myName, myModel gets 'dirty' (correctly).

我做错了什么?

谢谢

window.App = Ember.Application.create();
App.ApplicationAdapter = DS.FixtureAdapter.extend();

App.IndexController = Ember.ObjectController.extend({

});

App.IndexRoute = Ember.Route.extend({
    model: function() { 
        return Ember.RSVP.hash({
            myModel: this.store.find('myModel', 1),
            myOptions: this.store.find('myOption')
        });
    },
});

App.MyOption = DS.Model.extend({
    name: DS.attr('name')
});

App.MyOption.FIXTURES = [
    { name: 'User a', id: 1 }, 
    { name: 'User b', id: 2 },
    { name: 'User c', id: 3 },
];

App.MyModel = DS.Model.extend({
    myValue: DS.belongsTo('myOption'),
    myName: DS.attr('string')
});

App.MyModel.FIXTURES = [
    { 
        id: 1,
        myValue: 2
    }
];


<script type="text/x-handlebars" data-template-name="index">
    <h1>Test</h1>
    <lablel>My Value</label>{{input value=myModel.myValue.id}}
    <lablel>My Name</label>{{input value=myModel.myName}}
    {{view "select" 
        content=myOptions 
        selectionBinding=myModel.myValue 
        optionLabelPath="content.name"}}

    {{myModel.isDirty}}
</script>


推荐答案

ember-data不能处理关系的肮脏,然而。您需要在用户空间中实现它。

ember-data doesn't handle dirtyness for relationships, yet. You would need to implement it in user-space.


  • 这是相关问题:

  • 这个老问题专门讨论了belongToTo:

  • This is the relevant issue: https://github.com/emberjs/data/issues/2122
  • This old issue specifically discusses belongsTo: https://github.com/emberjs/data/issues/1367

这篇关于灰烬选择发生变化,但我的模型仍然“不脏”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 10:21