I am an absolute novice and have been working and struggling with ExtJS ! I am supposed to get a list of user records and dipslay them on Ext grid Panel. I have an ExtJS frontend and Grails ( Groovy Controllers ) backend. I have referred to a few links like:
http://docs.sencha.com/extjs/4.0.7/#!/example/grid/row-editing.html
http://docs.sencha.com/extjs/4.0.7/#!/example/restful/restful.html
http://docs.sencha.com/extjs/4.0.7/#!/example/writer/writer.html
The api property ( or ) tag ( or )attribute ( I don't know what it is called ) helps me in getting the list of JSON objects to be displayed in the Grid. Also, when I select a row and click on Delete, the request is reaching the delete action in my controller. But my problems begins here: how do I make sure that:
1) the selected row is deleted from Database? How do I pass the identifier or something to controller so that it will delete the record?
2) When I add a row, how do I pass the field values to backend Controller?
Most of the code is same as given in the restful link above. For reference, this is my Datastore:
https://docs.google.com/document/d/1gQyLCt6xWXTm-OUgYu7hku47r5WcS0my5yPBSKj2B7I/edit?usp=sharing
If you use a Rest proxy, ExtJS will auto generate the urls for you, based on the url stub that you specify. So if your proxy is configured to point to something like: /api/users, the following urls would be generated for each of the 4 actions:
read: /api/users (GET)
create: /api/users (POST)
update: /api/users/SomeIDFromTheUpdatedRecord (PUT)
delete: /api/users/SomeIDFromTheDeletedRecord (DELETE)
As you can see, the end point for each request is precisely the same (api/users), but for PUT and DELETEs, the id of the affected record is included in the URL automatically.
And of course, with POST and PUT requests, you can add any additional params that you'd like to send through to the server, although this will be done automatically when you persist the model instance via the store's configured proxy.
Related
I am sending requests to the the Firebase Realtime API. When I try to delete an from item within from a specific collection (e.g. journals) and specify which journal title using axios.delete("https://topxxx-xxx.firebaseio.com/journals.json", {title: "Nature"}}) all of the journals in the collection are deleted, not just "Nature". Similarly, if I do a get request and specify a title, all journals are returned.
I have also tried using "https://topxxx-xxx.firebaseio.com/journals.json", {params: {title: "Nature"}}. but that also returns all journals. Here are an example delete request:
axios.delete("https://topxxx-xxx.firebaseio.com/journals.json", {title: this.state.title})
.catch(error=> console.log("Error" + error))
The Firebase Realtime Database API doesn't support conditional deletes. To delete a node, you must specify the entire part to that node, and (in the REST API) either use the DELETE verb or use the X-HTTP-Method-Override: DELETE header.
So the two step process:
Perform a query to find the nodes with the title you're looking for:
https://topxxx-xxx.firebaseio.com/journals.json?orderBy="title"&equalTo="the title"
Delete the matching nodes by sending a DELETE to URLs like:
https://topxxx-xxx.firebaseio.com/journals/journalId1.json
You cannot delete a specify item from a json file like that in firebase, and also using GET will return the entire collection. It is working amazingly correct from firebase side. If you want something like how you are expecting, go for a small flask server or checkout cloud functions in firebase
You probably want to send that request to a back-end services that's connected to firebase (i'm assuming since you're using axios). Then call the remove method as demonstrated here
I am writing an app which will send orders to a remote server. I have a lot of the logic done now for setting up new orders. Items are added to a cart, cart totals are created and I am now ready to hit the server endpoint. At the moment, the REST API (which is being built by a separate team) needs me to:
Send a new order request and receive a new order number
Loop through my cart sending each item individually to the new order endpoint
Send the order totals
Send the payment options and amounts
Return the final data as a receipt to the customer
I currently have
- A cart collection which contains item models
- A totals model
I am not looking for code particularly but could someone outline a method to send the data to the server. I'm trying to figure how to use collections and API URI endpoints to do this but don't have any precedent to follow. Would it be natural in a Marionette/Backbone app to use direct POST requests to the server using defferds and promises or is there a better approach?
I would appreciate any pointers in the right direction,
Generally you wouldn't/shouldn't need to use direct POST requests when interacting with a REST API. Backbone models and collections are designed to interact with an API following this model out of the box.
If you define a collection as so:
var Items = Backbone.Collection.Extend({ url: '/items' });
var myItems = new Items();
myItems.fetch();
then when you call 'fetch' on the collection a GET request will be issued to your specified URL which will populate the collection with the models returned. You can add models to this collection which will fire the appropriate requests to the endpoint. eg. a POST. the default mappings are below for the above collection:
create -> POST '/items'
read -> GET '/items[/id]'
update -> PUT '/items/id'
delete -> DELETE '/items/id'
A lot of this is overridable and configurable so that you can fit into the API that your building against.
I'm using Google AppEngine as backend and AngularJS as front end for web application I'm making. I'm presenting data in pages to the user.
AppEngine has the ability to select data and return 3 pieces of information: the items selected, indication if there are more items and cursor for the next page.
I need to return all 3 pieces to the client app so it can present the fetched items and allow the user to go to the next page.
I also would like to use ngResource to interact with the server.
The problem is that ngResource expect the list of items to be a list and here it is an object with the 3 pieces.
Is there a way to modify ngResource a bit so that after fetching the data it will use the items to build the array of items?
Not necessarily, ngResource can deal with arrays as well as single item or json object. The standard get operation returns a object whereas query returns array. Bottom line as long it is a valid json data ngResource would work.
You can always call get on the resource, get the data into a json object and then it can have sub-properties which can be of array type.
You can share your specific structure and the community can help you with understand how to access it using ngResource
I have read in the cake book that Session in view uses SessionHelper, but that helper doesn't have method to delete session. Is this really so, could anyone help?
To delete session data, use the Session Component within a Controller, not a View.
//example from the CakePHP book (linked above)
$this->Session->delete('Person');
Or:
The destroy method will delete the session cookie and all session data
stored in the temporary file system. It will then destroy the PHP
session and then create a fresh session:
$this->Session->destroy();
While it's technically possible to delete session data in a view (it's just a PHP file after all), the CakePHP Framework was built with the MVC structure in mind. The "V" (for "view") should only be related to displaying the data provided by the Controller (retrieved from the Model).
The Model deals with accessing the data/database, and the Controller does the application logic. So - the Session Component (components are for controllers) is given a method to delete session data, but the Session Helper (helpers are for views) is not.
Based on what is the situation Custom Flash Messages can be used. Set flash message with custom key, like this
$this->Session->setFlash('my_value', 'default', array(), 'my_key');
and read it in view using flash method (which will automatically delete it)
$value = $this->Session->flash('my_key');
more details in this post http://hashmode.com/cakephp-delete-session-in-view/81
try unset($_SESSION['YOUR_SESSION_KEY']); in your view.
To delete a session variable, you can use the following code (in a Controller).
// same as unset($_SESSION['your_session_varable'])
$this->Session->delete('your_session_varable');
You can destroy all session variables calling the following:
$this->Session->destroy();
If the session data is an array and you want to access it just once just like the setFlash, you can set the array to setFlash with no template = false.
$this->Session->setFlash($arrayData,false,array(),'formData');
And then access it in the view
$data = $this->Session->flash('formData');
pr($data);
I'm just getting my head around BackboneJS, but one of the (many) things I'm still struggling with is how exactly the Models Sync up and relate to the serverside DB records.
For example, I have a Model "Dvd", now I change an attribute on the "Dvd" Model, the Name for example, and then I call save(), how exactly does the server side know what DB record to update? Does Backbone hold the DB row ID or something else?
Yes, typically you will set up your Backbone models so that they have database row IDs for any objects you're working with. When one of them is brand new on the client side and not yet saved to the server, it will either have a default or no ID, which doesn't matter since the server will be assigning the ID if and when the initial save transaction succeeds.
Upon saving or updating a model item, Backbone expects the server to reply with some JSON that includes any attributes that have changed since the save or update request was made. In the response to the initial save request, the server informs the client of a newly saved item's row ID (and you can also send along any other information you might need to pass to the client at the same time).
By default, the 'id' attribute of a model object is assumed to be its unique identifier, but backbone lets you change this if you're using a different identifier for the primary key. Just give your model an idAttribute parameter (see the docs for Backbone.Model.extend()) to do that.
Meanwhile, either the urlRoot parameter or a url function can be given to your models to characterize the urls that should be used to send the various ajax requests to the server for a given model.