im' trying to use a method which is in application.js, is these a controller.
it has a method called isAuthenticated which return if a user is login or not.
in my application.hbs it works fine,
but when i call it in my posts/new.hbs it always return false.
why it?
my application controller,
import Ember from 'ember';
import App from '../app';
export default Ember.Controller.extend({
authManager: App.AuthManager,
currentUser: function() {
return App.AuthManager.get('apiKey.user');
}.property('authManager.apiKey'),
isAuthenticated: function() {
return App.AuthManager.isAuthenticated();
}.property('authManager.apiKey'),
});
solutions?
Related
I have built a small Ember App. It has a controller which has an action named changeStatus(id). This method access the store and try to change an attribute value. I am relatively new to Ember. I don't know whether best way is to test it with Integration test or unit test? And how to test with either way.
Here is the sample code
Controller: user.js
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
changeStatus(id) {
const oUser = this.store.peekRecord('user', id),
let bCurrentValue = oUser.get('active');
if (bCurrentValue) {
oUser.set('active', false);
} else {
oUser.set('active', true);
}
//oUser.save();
},
}
});
Model: user.js
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
export default PatientIndexController;
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
export default Model.extend({
active: attr('boolean'),
});
Unit Test: user-test.js
import {moduleFor, test} from 'ember-qunit';
import Ember from 'ember';
moduleFor('controller:user', 'Unit | Controller | user', { });
test('Toggle user active status', function (assert) {
const contollerUser = this.subject();
contollerUser.send('changeStatus', 1);
assert.equal(contollerUser.store.peekRecord('user', 1).get('active'), 'true or false');
});
but I am not getting how to set some data to store. As when I call the function changeStatus() then it don't know about user model.
How to set a proper test. Thanks in advance.
this is my controller code
controllers/new-user.js
import Ember from 'ember';
export default Ember.ObjectController.extend({
actions:{
register:function(){
var person=this.store.createRecord('person'{
firstName:fname,
lastName:sname,
email:email,
password:password,
confirmPassword:confirmPassword
});
person.save();
}
}
});
I am working with ember-cli and i am new to ember so could you please tell me what is wrong in the code.
First there is to notice you should not use Ember.ObjectController but directly extend Ember.Controller. Checkout the deprecation.
Next all your variables fname, sname and so on are not declared. Probably you want to access the attributes on the controller. So either do this:
import Ember from 'ember';
const {get} = Ember;
export default Ember.ObjectController.extend({
actions: {
register(){
let person = this.store.createRecord('person', {
firstName:get(this, 'fname'),
lastName:get(this, 'sname'),
email:get(this, 'email'),
password:get(this, 'password'),
confirmPassword:get(this, 'confirmPassword')
});
person.save();
}
}
});
Or use .getProperties:
import Ember from 'ember';
const {get} = Ember;
export default Ember.ObjectController.extend({
actions: {
register(){
let person = this.store.createRecord('person', this.getProperties(
'fname',
'sname',
'email',
'password',
'confirmPassword'
));
person.save();
}
}
});
However my personal recommendation is to call createRecord in the model hook of the route and then directly bound your template to your model. Later just call .save() in your action.
I have a user service that manages reading data from localstorage, it has a property that reads from the local storage a certain object property, the service is setup like so
import Ember from 'ember';
import LocalUser from 'bidr/models/user-local';
const {
computed: {
alias
}
} = Ember;
export default Ember.Service.extend({
localUser: LocalUser.create(),
user_id: alias('localUser.user.id'),
active_auction: alias('localUser.user.active_auction')
});
In my route.js file for my item route I inject the service like so
user: Ember.inject.service('user'),
And in the template I'm attempting to access it like so
{{user.active_auction}}
I was under the impression I could do that but is that not the case? Do I need to set a property on the item route that is equal to the service property to make this work?
If you don't want to create a controller for this template's scope, you can expose the service to the template by setting a property in the auto-generated controller via the route's setupController hook, like so:
// application/some-route.js
export default Route.extend({
user: service(),
setupController(controller, model) {
this._super(controller, model);
set(controller, 'user', get(this, user))
}
});
Is there a more elegant way to prevent unauthorized access to an admin-only route than writing this in all of my admin routes?
export default Ember.Route.extend(AuthenticatedRouteMixin, {
beforeModel: function(){
if(!this.get('session.secure.admin')) this.transitionTo("dashboard");
}
});
Perhaps it's possible to extend AuthenticatedRouteMixin itself to make this kind of check?
Thanks!
Why not just make the mixin?
import Ember from 'ember';
import AuthenticatedRouteMixin from 'wherever/it/is'.
const { Mixin } = Ember;
export default Mixin.create(AuthenticatedRouteMixin, {
beforeModel(){
if(!this.get('session.secure.admin')) {
this.transitionTo("dashboard");
}
}
})
And then import it in your routes:
import Ember from 'ember';
import AdminCheckMixin from 'yourApp/mixins/routes/admin-check';
const { Route } = Ember;
export default Route.extend(AdminCheckMixin);
I have an application running with Ember 1.12.0. I have a simple template called /app/templates/repositories.hbs with this content :
{{controllers.user.login}}
I added this in the controller app/controllers/repositories.js :
import Ember from 'ember';
export default Ember.ArrayController.extend({
needs: "user",
});
I have this route :
import Ember from 'ember';
export default Ember.Route.extend({
model: function(params) {
var user = this.modelFor('user');
console.log(user);
return Ember.$.getJSON('https://api.github.com/users/'+user.login+'/repos');
}
});
And this router :
import Ember from 'ember';
import config from './config/environment';
var Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.route('index', {path: '/'} );
this.resource('user', {path: '/users/:login'}, function() {
this.resource('repositories');
});
});
export default Router;
According to the documentation. I should show the user login but I have just nothing. It show the user object in the console but not in the view.
Is the a solution ?
If I understand correctly, you have a user model fetched in user route (as you can see it in console, the modelFor works properly). However, you are trying to get login object on user controller - I think that it's not defined. What you really want to get is login property of the object that is the model for user route (probably it's the user, but you didn't posted your user route code).
To use these two models in one controller you don't need to share these controllers. All you need is the model for user route. Thus, you can leave your model hook as follows:
model: function(params) {
return Ember.$.getJSON('https://api.github.com/users/'+user.login+'/repos');
}
And write your setupController method as follows:
setupController: function(controller, model) {
controller.set("model", model);
controller.set("user", this.modelFor("user");
}
And in your template you would have your repos from AJAX and the user property with that property.
{{user.login}}
On the other hand, you could, with some fixes, use the shared user controller obtaining same effect, but it's not what you need - you need only the model for that controller, not the whole object.