Closed albhardy closed 9 years ago
Managed to resolve Adding Friend. Need to update following pages: models\friend.js
import DS from 'ember-data';
export default DS.Model.extend({
first_name: DS.attr('string'),
last_name: DS.attr('string'),
email: DS.attr('string'),
twitter: DS.attr('string'),
totalArticles: DS.attr('number')
});
controllers\friends\new.js
import Ember from 'ember';
export default Ember.Controller.extend({
isValid: Ember.computed('model.email', 'model.first_name', 'model.last_name', 'model.twitter', function() {
return !Ember.isEmpty(this.get('model.email')) && !Ember.isEmpty(this.get('model.first_name')) && !Ember.isEmpty(this.get('model.last_name')) && !Ember.isEmpty(this.get('model.twitter'));
}),
actions: {
save: function() {
console.log('+- save action in friends new controller');
if (this.get('isValid')) {
var _this = this;
this.get('model').save().then(function(friend) {
_this.transitionToRoute('friends.show', friend);
});
}
else {
this.set('errorMessage', 'You have to fill all the fields');
}
return false;
},
cancel: function() {
console.log('+- cancel action in friends new controller');
this.transitionToRoute('friends');
return false;
}
}
});
-form.hbs
<form {{action "save" on="submit"}}>
<h2>{{errorMessage}}</h2>
<p>
<label>First Name:
{{input value=model.first_name}}
</label>
</p>
<p>
<label>Last Name:
{{input value=model.last_name }}
</label>
</p><p>
<label>Email:
{{input value=model.email}}
</label>
</p>
<p>
<label>Twitter
{{input value=model.twitter}}
</label>
</p>
<div>
<h2>Friend details</h2>
<p>{{model.first_name}} {{model.last_name}}</p>
</div>
<input type="submit" value="Save"/>
<button {{action "cancel"}}>Cancel</button>
</form>
totalArticles should be changed to total_articles as well. Thus, in p.52 app/templates/friends/index.hbs
<td> {{friend.total_articles}} items</td>
and in p.56 app/models/friend.js
import DS from 'ember-data';
import Ember from 'ember';
export default DS.Model.extend({
articles: DS.hasMany('article'),
first_name: DS.attr('string'),
last_name: DS.attr('string'),
email: DS.attr('string'),
twitter: DS.attr('string'),
total_articles: DS.attr('number'),
fullName: Ember.computed('first_name', 'last_name', function() {
return this.get('first_name') + ' ' + this.get('last_name');
})
});
@albhardy I just tried the version I have in the book and it works fine, what I think is happening is that you probably forgot to define active model as the adapter? see https://github.com/abuiles/borrowers/blob/master/app/adapters/application.js
You're right - I might have overlooked that section on pg 15. The code works :) Thanks!
I downloaded the latest ePub (18 May) and noticed API changes for Friends from firstName into first_name. thus in pg.16
should be
I need to make similar changes in p.20, 22.
I'm currently stuck at Adding New Friend (Error
POST http://localhost:4200/api/friends 422 (Unprocessable Entity)
). However,http://localhost:4200/friends/2925
works.