ember adapter pass id - javascript

I want to be able to pass an id to rest point while using ember data. My end-point looks like v3/enterprise/inventory/items/{id}/links. I want to inject the id while making the request such as this.store.findAll('each-item-links', { id: itemId }). However, it does not work. I extended the Ember REST adapter and override the namespace but nothing seems to be working.

If you're trying to request a single record through Ember Data, then you want to use findRecord instead of findAll.
Also, if you need control over how the URL is built (what you have there looks like it might not map to the RESTAdapter too cleanly) you can override the _buildURL method to change the URL that the request is sent to. It is given the ID from findRecord so you can generate the URL whatever you want. Technically this is "private API" but I wouldn't worry too much about overwriting that.
Edit: To avoid using private API, there is also a public buildURL method that can be used instead.

Related

How can I change query params without a controller in Ember?

I have a callback from a library which returns data that I need to use to update query params. The problem is that this callback has no reference to any Ember structure or data. Is there a way to get access to the current controller to do controller.set(param, value) or perhaps a way of doing Ember.transitionTo({param: value})?
You don't need Ember to update the URL, you can do it from anywhere and Ember will detect the change and update the query parameters on the corresponding controller automatically. So in your case, just update the URL and Ember will know what to do. I was able to do this and it worked fine:
window.location.search = '?key=newvalue';

Using Breezejs generate query string

I have a custom Odata endpoint which gets it filter via a post from the body and not the query string. At the moment all of the queries are hand coded and then fed in to the post.
I was wondering if anyone knows if I can use Breezejs just to create the query I need.
Cheers
According to the "OData AJAX" section in Controlling AJAX you'll need to make changes to your copy of data.js so that POST is used instead of GET.
If you don't care to have breeze load the data and just want to use the EntityQuery syntax to build queries you could try getting an instance of the breeze odata uriBuilder and call the buildUri method to convert the EntityQuery to an odata uri. Assuming that works, it would be a matter of grabbing the querystring component of the uri to use in your POST body.
var interfaceRegistry = breeze.config.interfaceRegistry,
uriBuilderInterface = interfaceRegistry.uriBuilder,
uriBuilderCtor = uriBuilderInterface.getImpl('odata').ctor,
uriBuilder = new uriBuilderCtor(),
uri = uriBuilder.buildUri(entityQuery, metadataStore);
There might be a better/simpler way of doing this... maybe by grabbing the uriBuilder from a breeze odata dataService instance. I don't think this is part of breeze's supported/documented public api but it sounds like it would make a nice feature suggestion to post on the user voice site.

can I pass data when routing in angular

I want to pass a lot of data (json) when routing to a new controller in angular.
In controller A I call $location.path('/B'); which in turn will route to controller B.
I know I can pass parameter in the url itself, but I have a lot of data.
Can angular do something similar to 'POST' method and pass data in this way?
No need to bother with POST like behavior with angular.
You have several ways to do this :
use a service that will preserve data across page loads
pass real GET argument (when the page is specifically linked to this argument, for example an object ID used to display this object details)
store data in local storage / session storage
use controller 'resolve' functionnality to fetch new data before displaying page (not what you want to do though...)
Remember you're not actually changing the page, so you don't need to 'POST' data anywhere or do anything similar.
Instead you should create a service that makes that data available through dependency injection, then specify the dependency when you instantiate the controller that handles the new route.

Backbone without a datastore

I am working on an offline javascript application. It needs to support IE7 so localStorage is not an option. That said, the app does NOT need to persist any information (if you refresh everything gets wiped and that's OK).
So my question is, how do I set Backbone to just use a standard javascript variable (JSON) as my data store?
If I omit the model.url() method I get an error. I imagine this is simple, but I'm not sure what to do.
Thanks!
If you look at what the localStorage adapter is doing, you will find that it is overriding Backbone.sync. This is the module in Backbone which is responsible for storing/newing/retrieving/updating your data when you call new, save, fetch, etc.
By default, it uses a RESTful endpoint defined in the url of your model. If you use the LocalStorage override, it puts it in a local store.
If, instead, you just want to put it into an in-memory array, you would just override Backbone.sync the same way by defining what "read", "update", "create" and "delete" do. I would base it off of the backbone-localstorage.js adapter since it does most of what you want, but I would then store/retrieve from hash of id/object key/value pairs.
Simply do not use the save or create Collection methods.
Instead use store and add. These do not attempt to persist the data to storage.

Are there any Backbone.js tutorials that teach ".sync" with the server?

I read many Backbone.js tutorials, but most of them deal with static objects.
Of course, I have data on the server. I want a tutorial that shows how backbone.js can communicate with the server to fetch data, post data, etc.
This is .sync, right? I read the backbone.js documentation, but still fuzzy on how to use this feature.
Or can someone show me an example?
According to: http://documentcloud.github.com/backbone/#Sync
Backbone.sync is the function that Backbone calls every time it
attempts to read or save a model to the server.
But when? Where do I put the function? I don't know how to use it, and the documentation doesn't give any examples. When does the data get loaded into my models? I get to define when...right?
You never really have to look at .sync, unless you plan to overwrite it. For normal uses, you can simply call model.save() whenever you want and that will execute a post or put (depending on whether the record exists already). If you want to get the data from your backend, use collection.fetch()
You'll of course also need to specify a URL, do so through your collection attribute, collection.url
You can override Backbones native sync functionality if you override it:
Backbone.sync = function() {
//Your custom impl here
}
After that this function is called whenever you call a backbone function like .save() on models or .fetch() on collections. You do not have to care about data transport anymore.
I would suggest taking a look into Backbones source and look how the default sync function is implemented. Then create your own or adopt your server to support the native function.
They are not free, but the following screencasts both have a piece on backend work and how to send data to and get data from Backbone.
Tekpub is a 9 part screencast about asp.net MVC3, with the whole 6th part about using backbone to write an admin module to manage productions. it shows all about handling routing in MVC3 and sending & receiving data
Peepcode
http://peepcode.com/products/backbone-js about basic backbone stuff
http://peepcode.com/products/backbone-ii about interactivity
http://peepcode.com/products/backbone-iii about persistance (it's this third one you will need for server connection information).

Categories

Resources