2

抱歉,如果这个问题非常初级,但我是 Ember 的第一次用户,我正在开发一个应用程序。该应用程序会加载访问过的城市列表,所有这些都是显示推荐的链接。我加载城市列表并链接到它们没有问题。问题是单击没有建议的链接。我希望能够检测到没有针对城市的推荐并向用户显示消息。我不知道该怎么做。这是我当前的设置:

应用与存储:

window.TR = Ember.Application.create({
    LOG_TRANSITIONS: true,
    LOG_VIEW_LOOKUPS: true,
    LOG_ACTIVE_GENERATION: true
});
Ember.LOG_BINDINGS = true;

TR.Store = DS.Store.extend({
    revision: 12,
    adapter: 'DS.FixtureAdapter'
});

型号和夹具:

TR.location = DS.Model.extend({
    city: DS.attr('string'),
    state: DS.attr('string'),
    country: DS.attr('string'),
    visited: DS.attr('boolean'),
    recos: DS.hasMany('TR.recos'),
    locationName: function () {
        var ret = this.get('city');
        if (this.get('state') != null) ret += ', ' + this.get('state');
        return ret + ', ' + this.get('country');
    } .property('city', 'state', 'country')
});    

TR.recos = DS.Model.extend({
    type: DS.attr('string'),
    name: DS.attr('string'),
    address1: DS.attr('string'),
    address2: DS.attr('string'),
    city: DS.attr('string'),
    state: DS.attr('string'),
    country: DS.attr('string'),
    postalCode: DS.attr('string'),
    phone: DS.attr('string'),
    website: DS.attr('string'),
    location: DS.belongsTo('TR.location')
});

TR.location.FIXTURES = [
    { id: 1, city: 'Buenos Aires', country: 'Argentina', visited: true},
    { id: 2, city: 'Cordoba', country: 'Argentina', visited: false},
    { id: 11, city: 'Sydney', country: 'Australia', visited: false },
    { id: 3, city: 'Santiago', country: 'Chile', visited: true},
    { id: 4, city: 'Copenhagen', country: 'Denmark', visited: true },
    { id: 5, city: 'Paris', country: 'France', visited: true },
    { id: 6, city: 'Bombay', country: 'India', visited: false },
    { id: 7, city: 'Delhi', country: 'India', visited: false },
    { id: 8, city: 'Kolkata', country: 'India', visited: true },
    { id: 9, city: 'Mexico City', country: 'Mexico', visited: false },
    { id: 10, city: 'Bangkok', country: 'Thailand', visited: false }
];

TR.recos.FIXTURES = [
    {
        id: 100,
        type: 'Restaurant',
        name: 'Chez Louis',
        address1: '123 Main St.',
        address2: '1st Floor',
        city: 'Buenos Aires',
        country: 'Argentina',
        postalCode: '12345',
        phone: '2125551234',
        website: 'http://www.chezlouis.com',
        location_id: 1
    },
    {
        id: 101,
        type: 'Hotel',
        name: 'My Hotel',
        address1: '345 Main St.',
        city: 'Buenos Aires',
        country: 'Argentina',
        postalCode: '12345',
        phone: '2125551234',
        website: 'http://www.myhotel.com',
        location_id: 1
    }    
];

路线:

TR.Router.map(function () {
    this.resource('visited', function () {
        this.route('recos', {path: '/recos/:location_id'});
    });
});
TR.VisitedRoute = Ember.Route.extend({
    model: function () {
        TR.location.find();
        return TR.location.filter(function (location) {
            if (location.get('visited')) { return true; }
        });
    },
    renderTemplate: function (controller) {
        this.render('locations', { controller: controller });
    }
});
TR.VisitedRecosRoute = Ember.Route.extend({
    model: function (params) {
        return TR.recos.find(params.location_id);
    }
});

控制器:

TR.LocationController = Ember.ObjectController.extend({
    showRecos: function (location, controller) {
        controller.parentController.transitionToRoute(controller.parentController.prefix + '.recos', location.get("id"));
    }
});
TR.LocationsController = Ember.ArrayController.extend({
    addLocation: function () {
        var locationVal = this.get('newLocation');
        if (!locationVal.trim()) { return; }

        var location = TR.location.createRecord({
            name: locationVal,
            visited: this.get('visited')
        });

        this.set('newLocation', '');

        // Save the new model
        location.save();
    },
    itemController: "location"
});
TR.VisitedController = TR.LocationsController.extend({
    heading: "Places I've Visited",
    prefix: 'visited',
    visited: true
});

因此,使用上面的代码,应该只显示布宜诺斯艾利斯的建议。对于其他城市,应该显示一条消息,告诉用户没有推荐。

4

1 回答 1

0

您可以挂钩该VisitedRecosRoute afterModel函数并检查您的 find 操作是否返回了建议。如果没有,请显示您的信息:

TR.VisitedRecosRoute = Ember.Route.extend({
  model: function (params) {
    return TR.recos.find(params.location_id);
  },
  afterModel: function(recos, transition) {
    if (recos.content.length <= 0) {
      transition.abort();
      // show your message
      // ...
    }
  }
});

希望它有帮助。

于 2013-08-24T09:28:30.830 回答