Ionic 1.6.5 and Updates to Services
The Ionic team released an update to the Ionic CLI. Get the lowdown on all of the improvements.
Join the DZone community and get the full member experience.Join For Free
as a quick aside, i worked with eric bobbitt of the ionic team while writing this. i asked him a lot of questions very quickly and he never complained. big kudos to him for being patient with me!
a while ago i gave a presentation on ionic services, and as i mentioned earlier this week, i’ll be speaking on the topic again next month . today the ionic folks released an update to the cli that helps improve ionic service usage as well. this ties in with other improvements they have made since i last dug into them. the services are still in alpha, which is bad news on one hand (i can see being concerned about using them in a production app), but the good news is that they have gotten a heck of a lot easier to use since i last played with them. in today’s post i’m going to talk about those setup changes as well as show some updates they’ve done to make working with users easier.
first and foremost, you want to update your local ionic cli. type
at the command line and you’ll probably see this:
go ahead and run the update and ensure you are at version 1.6.5. at the time i wrote this blog entry the change log had not yet been updated, but hopefully that will be corrected soon.
next, you’ll want to ensure you have an account at ionic.io . i’m not entirely sure if the cli allows you to register, but since you’re going to be using the site anyway, you should go ahead and register there.
at this point, i’m going to borrow directly from ionic’s excellent docs for their services. you can find them here: http://docs.ionic.io/ . but i’m copying from them because i’m so darn excited about how simple things have gotten. assuming you’ve made a new project, you begin by adding the core services library like so:
ionic add ionic-platform-web-client
this will automatically modify your index.html and app.js. yes, if you rename your files this won’t work correctly, but a) don’t do that and b) you can still manually modify the files if you want.
next, you need to upload your application to ionic.io:
ionic io init
the first time you do this, the cli will prompt you to login. after that it will remember your authentication information.
and that’s it. seriously. previously this involved you doing all the edits by hand. to be clear, it wasn’t a lot of work – we’re talking 5 minutes here – but i know i screwed it up more than once and i freaking love that the cli is handling this for me now.
all in all, services are a heck of a lot easier now and that’s pretty freaking cool. now let’s turn our attention to the user side. the docs on users are a bit bare now. personally, it feels like ionic users aren’t necessarily a real “service” per se but more of a “support system” for the other services. that’s my gut feeling now and obviously i reserve the right to change my mind later.
ionic users have a couple of different features:
you can easily define and create a new user for your application. if you don’t want to manage ids, you can use
ionic.user.anonymousid();to have an id assigned.
- you can “save” your user so that ionic.io knows about it as well. (more on that in a minute.)
- you can set and get basic values on users. ionic users will have a few internal properties like id, image, and is_deleted. these are not documented currently but will be. also, the property api (next bullet) does not work with these values so it isn’t something you have to worry about running into. (although you will want to read the next bullet carefully.)
to get a value, you just do
user.get("key"). you can also provide a second argument for a default value. setting works just as easily:
user.set("key", value). you can provide complex data like arrays and it will persist just fine. as i said, the internal values are not fetched this way. you may want to get the id value. to do that, you simply get it on the core user object:
- now here is where stuff gets cool. you can also register your own data types to add complex support for user data. if you register an object that has a tojson and fromjson method, you can then create a user property of that type and store more complex data with additional rules. ionic is shipping one by default, uniquearray (and yes, i’ve told them to be sure to document all the defaults), that provides support for storing arrays of unique values.
to save a user, and you
save users if you want the data to exist on ionic.io, you simply do
user.save(). this is an async call but you don’t have to handle the results unless you care too. note that users are saved to localstorage. if you are offline, the save happens, but does not persist to the server (obviously). in that scenario, if you run
user.isdirty()you will get true back. how you handle this is… well i guess up to you. don’t forget that going online fires an event you can listen for, so in that event you could check isdirty and try running save again.
here is where things get a bit complex. i’m going to show the web ui in a moment. it is possible that the server may have newer data for the user than your application does. ionic provides a
loadevent so it is easy to say, “hey, get the user from the server and make him my local user”, what isn’t necessarily obvious is how/why you would do that. the web site, and the rest api (which isn’t public yet), will provide read/write access to users. so your app may have some node.js back end that, once a day, updates users. therefore your code may need to run a
loadon startup to ensure the local user matches the remote server. how and when and how anal you get about this really depends on the application i suppose. also, it is entirely possible that you will never modify the user outside the application.
ok, so how about the web site of this. first off, don’t forget the big white bar on top of the ionic.io site is a search bar. i know i forget sometimes, and if you are like me and have about two hundred apps, it can be difficult to find the one you just added.
yes, it clearly says search – i still miss it. once you find your app, simply click the users link.
you can see i’ve got a grand total of one user. in the future, ionic plans on adding basic search/filter so you can find one user among millions. and of course, one day we’ll have a proper rest api for everything on ionic.io. clicking edit lets you add arbitrary values to the user:
and yes – i did try to inject something bad into the user project. even with stuff i love i try to see if i can break the web app. if you do add properties via the web form, it is vital that you do the following. first, click done:
and then – you must hit done again!
you may or may not see this bug/issue. eric and i are talking about it right now, and he believes it is an ajax timing issue. so if you click around crazy like i tend to do, you may see it more often. just… look out for it and hopefully it won’t be an issue in the future.
so – that’s the user service. it also connects to both the push and analytics services as well. i think once a proper rest api comes in this service will stand up a bit more on its own though. let me know if you have any questions and don’t forget to peruse the docs . i’m also thinking about a full demo with users and analytics for sometime this week (or next).
Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.