2013-08-24 36 views
2

道歉,如果這個問題是非常基本的,但我是第一次Ember用戶和我正在一個應用程序。該應用程序加載訪問城市的列表,所有這些都是顯示建議的鏈接。加載城市列表並鏈接到它們,我沒有問題。問題是點擊沒有建議的鏈接。我希望能夠檢測到沒有針對城市的建議並向用戶顯示消息。我無法弄清楚如何做到這一點。這裏是我的當前設置:在沒有模型的情況下,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 
}); 

所以,使用上面的代碼,只有布宜諾斯艾利斯的建議應該顯示。對於其他城市,應該顯示一條消息告訴用戶沒有建議。

回答

0

您可以掛鉤VisitedRecosRouteafterModel函數並檢查是否有您的查找操作返回的建議。如果沒有,請顯示您的消息:

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 
     // ... 
    } 
    } 
}); 

希望它可以。

+0

非常感謝。這對我有效,但我不得不將'recos.length'更改爲'recos.content.length'。我非常感謝幫助。 – user2689570

+0

@ user2689570,很高興我可以幫忙,編輯我的答案以反映更正,如果您願意,可以接受它,因此它被標記爲已回答,謝謝 – intuitivepixel

相關問題