I was trying to use loopback-component-storage. It's all working fine.
The one thing that I cannot figure out is how do I observe a file upload completion within loopback, when the following API is called:
http://localhost:3000/api/Epubs/{container}/upload | POST
I tried using afterRemote(".") & observe("access"). None of them seem to trigger.
You can use afterRemote on method 'upload'. In your case that will be
Epub.afterRemote( 'upload', function( ctx, modelInstance, next) {
...
next();
});
See remote hooks docs
Related
I have a use case where the action should get the value from another component, based on that I need to do some actions.
Initially, I used sendAction (instead of promiseAction()) to do some actions. But closeDataModal() runs immediately after finishing the sendAction. I want the first function to finish up and then run the second one.
saveAction() {
promiseAction()
closeDataModal() -> Run after resolving the promiseAction
}
Use an async function so that you can await an async operation like an async action.
async saveAction() {
await promiseAction()
closeDataModal() -> will run after resolving the promiseAction
}
If you want to use the result of promiseAction then:
async saveAction() {
let result = await promiseAction()
closeDataModal(result) -> will run after resolving the promiseAction
}
As mentioned in the comments, this will not work with sendAction which is deprecated.
See https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/async_function for more infos on how async/await works and how error handling is done.
I am trying to do the following thing:
I have a model, say myModel which has some method calculateSomething. I defined that function by writing something like this in the MyModel.js file:
MyModel.prototype.calculateSomething = function(cb){
...
return cb(null,result)
}
Now I want to include the result of calculateSomething in the json whenever an instance of MyModel is returned from the api.
How do I do this? I tried using the "loaded" hook, but I believe this hook gets executed before the MyModel instance is created, so I can't call the calculateSomehing method there.
EDIT: It turns out that I can just use the "loaded" hook. I can use the ctx.instance in the hook to get the object.
I was confused by the documentation :
"LoopBack invokes this hook after the connector fetches data, but before creating a model instance from that data". Is the documentation wrong or am I misunderstanding it?
How about using Remote Hooks (on mymodel.js):
// run before any static method eg. MyModel.find
MyModel.beforeRemote('*', function(ctx, myModel, next) {
myModel.calculateSomething(function(err, something) {
if (err) throw err
myModel.something = something
next()
})
});
OR
If you need to do it on object initialization phase (while operation hook loaded seems to be not working) maybe you can try the model hook afterInitialize assuming no async call invoked from calculateSomething:
MyModel.afterInitialize = function() {
this.something = this.calculateSomething(function(err, result) {
return result
})
}
OR
As discussed below, if you need do async call and/or want to have this logic on subclasses, I think you should consider implementing createSomething not as object/prototype method but as a mixins. I haven't tried this personally though but it looks quite suitable to your need.
I have a simple question. Whats the main difference doing a MyModel.beforeRemote('create') hook for a create method and a MyModel.observe('before save'). I already read the docs and I know that operation hooks are not tied to a particular method, but rather are triggered from all methods that execute a particular high-level operation (ex. create). But in this particular example, MyModel.beforeRemote('create') will work as same as I do MyModel.observe('before save'), right? Or this will execute on other "state" of the api flow?
Remote hook:
MyModel.beforeRemote('create', (ctx, next) => {
console.log("beforeRemote");
next();
}
Operation hook:
MyModel.observe('before save', (ctx, next) => {
console.log("before save");
next();
}
MyModel.beforeRemote('create') would only be invoked for the 'create' remote method, but MyModel.observe('before save') would be invoked for any of these:
create
upsert
findOrCreate
updateAll
prototype.save
prototype.updateAttributes
See the table here for all the remote methods that would invoke each operation hook: https://docs.strongloop.com/display/APIC/Operation+hooks
When upgrading EmberData from 1.0.0-beta.9 to 1.0.0-beta.10 I've noticed that the same OPTIONS/GET request is made multiple times when using a computed property in various places of the page.
Sample Code
http://jsbin.com/moruj/1/
Then in my request, I see 3 option calls to my events API. This wasn't happening in beta.9 so I'm curious what may have happened?
Before answering your question, I have to say the code in your question is wrong. Please check and modify your code so more people can understand it.
For example in your code:
/* It should be App.Customer right? */
App.CustomerModel = DS.Model.extend({});
/* should be App.CustomerIndexRoute ? */
App.CustomerIndexController = Ember.Route.extend({
model: function(params) {
/* it returns a promise which resolves a customer array */
return this.store.find('customer');
}
});
App.CustomerIndexController = Ember.Controller.extend({
/*
* But for a customer array you can not get events property.
* you can only get events from a single customer.
*/
filteredEvents: function() {
return this.get('events').slice(0, 10);
}.property('events')
});
For multiple GET requests for events
No matter how you construct your route & controller code, when you iterate events for a customer, Ember Data sends a request /events/:id for each un-fetched event. You can set coalesceFindRequests: true in your adapter to load events in one request, like /events/ids[]=1&ids[]=2&ids[]=3.
For multiple OPTION requests
Please check if you use proxy to delegate ajax requests to a remote server or a mock server. As I know an OPTION request will appear when your proxy can not delegate the request correctly. In this case you may see an OPTION request and a GET request at the same time. It's not an Ember Data problem.
It looks like this was a legit but in beta.10
http://discuss.emberjs.com/t/requests-triggering-multiple-times/6316
I need to run some code after ember application got initialized. I don't want to invoke this code from App.ready to avoid tight coupling. It would be nice to have something like this:
App.on 'ready, -> console.log('do stuff')
But it won't work since Em.Application object is not subscribable and ready isn't really an event despite that docs said so
A simple way you can achieve this would be to extend your Application class with the Ember.Evented mixin:
App = Ember.Application.createWithMixins(Ember.Evented, {
ready: function() {
console.log('App ready');
this.trigger('appReady');
}
});
And you hook inside the ready event inside your app and trigger your custom event using this.trigger(...)
At this point you can use .on(...) to be notified when the event is triggered.
App.on('appReady', function() {
console.log('App already ready');
});
Example demo.
Hope it helps.
An other possibility may be to invoke your code from the resolve callback of the application.
App.then(function(app) {
console.log("App is resolved, so it's ready");
});
example stolen from #intuitivepixel ;) http://jsbin.com/AdOVala/66/edit
Edit/Note:
App.then() has been deprecated, see http://emberjs.com/deprecations/v1.x/#toc_code-then-code-on-ember-application:
As part of the Ember.DeferredMixin deprecation, using .then on an
Ember.Application instance itself has been deprecated.
You can use the ready hook or initializers to defer/advance readiness instead.