Angular Model From Server - angularjs

I'm new to Angular and I just can't get my head wrapped around this idea, any help would be greatly appreciated.
A lot of conversations state the Model should come from the server via restful web services. I've been using $http in a factory. This makes sense to me "if" there is data present. If you load a screen and the user or whatever is new then you get a blank JSON value. For complex data (relationships) you get those items with a value but other properties are left off.
So what am I missing here, how can the model come from the server consistently?

It's useful to think of your model as both a server model and a client model. The server model should be your true model or "source of truth", and the client model is a working model or "mimic" that should behave as a local copy of the server model.
For the model to "come from the server consistently", you have to ensure that any changes to the client model get validated by the server side. Meaning that when any change requests to the model -- such as create, remove, update, or delete (crud) -- get sent as requests to the server, and then the resulting changed data model gets returned to the client model so it can be updated.

You could take advantage of standard HTTP status codes as a mechanism to provide results to the client:
for example, your service could return an HTTP code of 204 to indicate that the server successfully processed the request, but is not returning any content.

Related

Is it possible to update an AngularJS expression via an API call through a Service?

I'm looking for a way (and not even sure if this is possible) to update an Angular expression on my HTML page when data is sent to an API.
For example, say I have $scope.message on my .html page. Is there a way I can send a message to an API (e.g. http://...?message=foo) and have my page update with the message sent?
Also, I need the $scope.message to be updated in an Angular Service so it is available to multiple pages within my website.
I am wanting this to be a live update, but if not, I am happy with some code executing on a timer or something similar.
Any suggestions appreciated.
UPDATE
I'm guessing it may not be possible, but just in case I haven't explained it correctly, I'll try and simplify it.
I can only find information about using AngularJS to send GET commands OUT to a URL and receive data back. I need to send a JSON string TO my Angular site to update a variable. So basically as a field updates in my database, I want another server application to send an alert to my Angular site to update the status of this value live. I don't really want to run a constant check of the database if I don't have to.
I am open to any other suggestions on how to achieve this.
if you don't care about old school browsers, you can try using WebSocket, it is similar to a TCP socket, which allows you to push from server to client. I would create a service that manages the websocket connection, and update scope when receiving message. Hope this help. More info here: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

How to refresh the website with Nancy if model data changed?

If my model data is changing how to refresh the site automatically to display the changed data?
Is this possible at the server or do I have to write some client code for this?
Thanks for helping.
Michael
You have to write client side code to do this, the server has no relationship with the client once its sent the response - the connection is closed, the server moves on to other requests.
You need to use Javascript to poll the server and update based on server side changes - you can use a data binding framework such as Knock Out, or a larger framework such as Angular to make this easier.
SignalR may help you here, in that its a system which plugs into both the client and server side, keeps open a connection so you can send data to the client side instantly from the server.

How to receive postback using Express and Angular?

I have a server written in Express that interfaces with an application written in Angular on the client.
My Express server is receiving a post from a third-party service to a route which will perform business logic, and then here is where I am a little uncertain about the best path forward.
After receiving the post variables, I want to redirect the request to an Angular route, but I want to make those received post variables available to the route as well.
Somehow, I want to be able to mix the res.json() and res.redirect() method, but I'm pretty sure they both end the response.
What would be a logical way to structure this?
Update: To expand on the issue, imagine I have a route called /receivetransaction which receives some postback variables, including transaction ID, amount etc. I want to perform business logic (save to a database), and then redirect the user to /thankyou (an angular route) but have them be able to access that data that was just received in the postback.
It looks like maybe my best option would be to save to the database, and then send the transaction-id as JSON to the angular view, which will then hit the database and pull the info. A little inefficient though (not really a big deal) but I would hope there would be another way around it.
What I've decided to do is the following:
After the express route receives the postback variables, it performs the business logic (specifically saving the data to the database), and redirects the request to the angular route with a query var indicating the id of the transaction.
The angular controller uses $location.search() to pull the transaction id from the query var, and from there it performs a get request to the express API, which performs authentication and loads the relevant information into $scope variables to be passed to the view.

Sails server side views and socket for client updates (in angular)

Struggling to get my head around this functionality.
I use a sails server view to host a simple find for a given model in the DB.
When browsing to: url/model I get the html view that populates the data.
So far so good.
My view is handled by angular and have databinding between the visible data and what came from the server (ejs).
Now, I want the data to change dynamically with sockets on the client view.
Reading the code and the docs, it seems I need to have the client view, actually get the data to be able to subscribe to it on the server side. (This handled in blueprints, or manually if I want to in a controller method).
The problem is that I will be querying the data twice in the DB.
Once in the server hosted view with the default HTTP handled blueprint, and then
Twice when I actually have to get the data via sockets.
I managed to only do this once, if my client is standalone, by simply querying the data via sockets only.
But how can I do both ? Server hosted views and data, with subscription to sockets for updates, without having to get the data twice ?
Thx.

Backbone.js: Multiple delete with 1 request

I have a backbone.js application connecting to a REST API. I noticed that if you delete multiple models at once, seperate API request have to be sent for each model.
Is there any way to handle the delete request using 1 request?
You would need your server to expose an endpoint for deleting multiple models at once by passing IDs of the models to be deleted in the first place. If you have this available the common way to handle that would be to add a method to your collection called something along the lines of deleteByIds which would accept array of IDs and then this method would remove the models from the collection on successful delete request (if sync) or straight away before sending the delete request to the API endpoint which would make sure they are all removed from the server.
By default that's how RESTful interfaces work and batch processing is always a custom extension to RESTful interfaces so there is no out of the box way to do that and it might involve you doing some extra work both on the backbone client and on the backend.

Resources