Ember.js dynamic model - javascript

I'm looking into building an application with Ember.js but I'm not sure about whether its model support is appropriate for my needs. I'm working on a database front-end application which connects to a central server; the main problem here is that the database definition might change (i.e. removing or adding columns).
Editing the code manually to accomodate all changes to the database is something I'd like to avoid at all cost. Therefore, I'd like to know if Ember.js provides a way to generate dynamic models. I've come across the defineProperty method but I haven't been able to figure out how it works, neither have I found anything about it in the Ember.js docs. Besides, it seems to be necessary to run it once for each object; is there a way to run it just once?

A possible approach may be to automate the generation of your client side schema from your database schema.
A similar solution has been demonstrated here (though the implementation is Rails specific) : http://techblog.fundinggates.com/blog/2013/03/automatically-generate-ember-models-from-rails-serializers/

Related

When should I use an MVC framework in JavaScript?

I know this topic is likely to become subjective, therefore it is not about my particular web application I am working on.
For JavaScript, I haven't worked with MVC libraries like Backbone.js but I see the advantage in decoupling logic and views. On the other hand, it may just not be worth the time to learn the framework and adapt the application to make use of it. Moreover, handling all views in JavaScript makes SEO much harder, I guess.
So how should I decide whether it makes sense to use Backbone.js or a similar framework given the concept of a web application? On what depends the decision?
Any help to make the question more objective is welcome.
There are many client side javascript MVC (or MV*) frameworks out there. Most seem to have a different idea of what MVC is and how it should work together with your web application.
You should consider an MV* framework if the complexity in your client side javascript is becoming difficult to manage. If you have a team building a highly dynamic single page application (SPA) that does a lot of asynchronous communication with the backend then you will want to consider it. It's easy to end up with very messy javascript code otherwise.
An MV* framework will help you in one or more of the following ways:
by defining how the code should be structured. This is done to various degrees depending on how opinionated the framework is. Backbone for instance considers itself a library rather than a framework and thus leaves more of the decision making up to the user
by binding html to model. So if your data changes the page will be automatically updated (and vice versa)
by providing useful features such as URL history (for single page apps) and validation
I think it depends on your project. Here is a checklist which can help you in deciding whether you should use frontend MV* framework or not.
Lots of ajax request to backend
Many of your functionality doesn't require full page reload. Like adding a comment, pagination or infinite scroll.
You have models/REST API at backend. You can just replicate/use same structure at front end.
You are sharing logic/dom manipulation functions across different pages.
I will add more if I find any more points. This is what I can think right now and this is not a complete list. Any suggestions are welcome.
As You Know MVC structure can be included in javascript by using
backbone.js with
underscore.js and other similar libraries mainly focusing on each form field as property in an entity such that entities can be used different purposes MVC structure is maintained for easy manipulation in javascript
These structure good for event binding,dom manipulation ,serialization,etc
It depends on application's purpose for choosing which structure suitable whether its mvc or mvvm structure for javascript. MVVM structure can be included in the javascript through angular.js or knockoutjs or other libraries for dynamic binding to view component Javascript can be used
MVVM architecture will be usefull for maintaining form field binding without reloading or ajax There are a lot of application by maintaining the architecture in js and i have pointed out only a few .
for eg : Serialize form inputs to JSON using Backbone.js
In this example the form values are retrieved as model and then it can be manipulated and can add business logic and can be serialized or event binding and can do print or any thing

Migrating from asp.mvc application to node.js application with a focus on design [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I am currently looking into alternative platforms to migrate an existing application onto, it started out as a prototype using asp.mvc but the majority of code is javascript with a simple asp mvc web service so as we are looking at taking it forward it seems sensible that we just scrap the current microsoft stack and just go for nodejs giving us more freedom of where and how we host our application, also we can reuse some of the models and code in both the web service and front end, although this would probably end up being a small amount.
This will probably be quite a large question encompassing many parts but I will put it out there anyway as I am sure this could be helpful to lots of other people looking at how they can move from something like .net/java to to node.js. As most of these statically typed languages have lots of patterns and practices used, such as Inversion of Control, Unit of Work, Aspect Oriented Programming etc it seems a bit strange moving towards another platform which doesn't seem to require as much structure in this area... So I have some concerns about migrating from my super structured and tested world to this new seemingly unstructured and dynamic world.
So here are the main things I would do in MVC and would want to do in node.js currently but am not quite sure the best way to achieve the same level of separation or functionality.
Routing to Actions
This mechanism in ASP MVC seems to be replaceable by Express in node.js, which will give me the same ability to map a route to a method. However there are a couple of concerns:
in ASP MVC my controllers can be dependency injected and have variables so actions are easy to test as everything they depend on can be mocked when needed and passed in via constructor. However as the method in express seems to not have a containing scope it seems like I would have to either use global variables or new up the variables internally. Is there a nice way to access my business logic containers in these routed methods?
Is there any way to autobind the model being sent? or at least get the json/xml etc in some useful manner? It appears that if you send over the correct mime type with the content it can be extracted but I have not seen any clear examples of this online. I am happy to use additional frameworks on top of Express to provide this functionality, as ideally I just want to make a POST request to /user/1 and have the user object pulled out and update the user with id 1 in the database (which we will talk about shortly).
What is the best way to validate the data being sent over? Currently in our front end javascript application we use KnockoutJS and all models use knockout observables and are validated using Knockout.Validation. I am happy to use Knockout Validation on node as the models are the contract between the front end and back end, however if there is a better solution I am happy to go look into it.
Database Interactions
Currently in .net land we use NHibernate for communicating with our relational databases and the MongoDB driver for communicating with our MongoDB Databases. We use a Generic Repository pattern and isolate queries into their own classes. We also use a Unit Of Work pattern quite heavily so we can wrap logical chunks of word into a transaction and then either commit it all if it goes well or roll back if it doesn't. This gives us the ability to mock out our objects at almost any level depending on what we want to test and also lets us change our implementations easily. So here is my concern:
Sequalize seems to be a good fit for replacing NHibernate, however it doesn't seem to have any sort of transaction handling making it very difficult to make a unit of work pattern. This is not the end of the world if it cannot be done in the same way, but I would like some way of being able to group a chunk of work in some way, so an action like CreateNewUserUnitOfWork which would take a model representing the users details, validate it, create an entry in one table, then create some relational data in others etc, get the users id from the database and then send that back (assuming all went well). From looking at the QueryChainer it seems like it provides most of the functionality but if it failed on the 3rd of 5 actions it doesn't appear simple to roll back, so is there some way to get this level of control?
Plugins / Scattered Configuration Data
This is more of a niche concern of our application, but we have the central application then other dlls which contain plugins. There are dropped into the bin folder and will then be hooked in to the routing, database and validation configuration. Imagine it like having the google homepage, and google maps, docs etc were all plugins which told the main application to route additional calls to methods within the plugin, which then had their own models and database configurations etc. Here is my concern around this:
There seems to be a way to update the routing by just scanning a directory for new plugins and including them (node.js require all files in a folder?) but is there some best practice around doing this sort of thing as I don't want each requestto have to constantly do directory scans. It is safe to assume that I am happy to just have the plugins in their right places at the time of starting the node application so no need to add plugins at runtime at the moment.
Testing
Currently there is Unit, Integration, Acceptance testing within the application. The Unit tests happen on both the front end and back end, so currently it will do javascript tests using JsTestDriver in our build script to confirm all the business logic works as intended in isolation etc. Then we have integration tests which currently are all done in C# which will test our controllers and actions work as expected as well as any units of work etc, this again is kicked off by the build script but can also be run via the Resharper unit test runner. Then finally we have acceptance tests which are written in c# using web driver which just target the front end and test all the functionality via the browser. My main concerns around this are:
What is the best practice and frameworks for testing nodejs? Currently most of the tests which test at the ASP layer are carried out via C# scripts creating the controller with mocked dependencies and running the actions to prove it works as intended using MVC Helpers etc. However I was wondering what level of support NodeJS has around this, as it doesn't seem simple (on a quick glance) to test node.js components without having node running.
Assuming there is some good way to test node.js can this be hooked into build scripts via command line runners etc? as we will want everything automated and isolated wherever possible.
I appreciate that this is more like 5+ smaller questions rolled up into one bigger question but as the underlying question is how to achieve good design with a large node js application I was hoping this would still be useful to a lot of people who like myself come from larger enterprise applications to node js style apps.
I recently recently switched from ASP MVC to Node.js and highly recommend switching.
I can't give you all the information you're looking for, but I highly recommend Sequelize as an ORM and mocha (with expect.js and sinon) for your tests. Sequelize is adding transaction support in the next version, 1.7.0. I don't like QueryChainer since each of it's elements are executed separately.
I got frustrated with Jasmine as a test framework, which is missing an AfterAll method for shutting down the Express server after acceptance tests. Mocha is developed by the author of Express.
If you want to have an Express server load in plugins, you could use a singleton pattern like this: https://github.com/JeyDotC/articles/blob/master/EXPRESS%20WITH%20SEQUELIZE.md
You will probably will also really like https://github.com/visionmedia/express-resource which provides a RESTful interface for accessing your models. For validation, I think you'll be happy with https://github.com/ctavan/express-validator
Why would you need to test Node modules without using Node? It's considered standard to call a test script with a Makefile, and you can add a pre-commit hook to git to run the tests before making a commit.

What Javascript framework is best for this situation?

I want to build a contact list in js, but there are so many js technologies like backbone.js, spine.js, knockout.js, etc. that I dont really know which one is best for me.
My contacts have a few overarching types: FacebookUser, User, and UserList. The catch with userlist is that it is a collection of users or UserLists. Furthermore, it itself may or may not have its own contact information (like a company or a team).
I want to display this list in a tree-view where you can drill down into UserList (and have the group members lazy loaded).
Lastly, the entire contact list is an instance. What I mean is that you have have 0-n number of contact lists open in the same window at a time.
What framework should I use for this situation?
All three of the libraries that you mention (backbone.js, spine.js, knockout.js) are aimed at single-page browser apps.
But your description of your problem sounds more like a formatting issue, not data management. Ie, you want your page to show the data. You don't need to save changes from your page back to the server; your users will refresh to the page to show new or different information.
If my understanding of your goal is correct, I'd go with jQuery or YUI. The libs you mention would be overkill.
Added YUI has a nice treeview widget that supports lazy-loading of branches. I use it for that.
If you do decide to use a model layer, YUI 3 includes Model and ModelList. They're based on Backbone api.
Re: MVC comment In a comment, the OP mentions "multiple views on the same data, change propagation, and edit detection (if a user changes a number) and updates from the server"
For those cases, yes, these days many cool kids are using a complete MVC framework on browser itself. If you wish to go in that direction, then you could use any of the three you mentioned. Notes:
Backbone, and probably the other two, do not provide complete MVC. Rather, Backbone supplies the Model & Controller portions, especially the Model. It is common to use Mustache for the View. In addition, it is common to use underscore or jQuery to supply basic facilities. If you Google for backbone examples, many of them include jQuery.
Checkout YUI new y.App. It includes all of the MVC parts in one well documented lib. y.App is in hot development and is based on the Backbone api. A video-cast
MVC Frameworks on the browser are brand new. Don't be surprised by quickly changing library code, inconsistent docs, few examples, etc. You should also consider documenting and posting your investigations. For extra credit, get it working well on mobile browsers.
49 Signals was rumored to be working on a browser-side MVC framework, but we haven't seen anything yet.
I believe that backbone is the most senior of the Model libraries. But it has proven to be confusing to many new users, hence some of the other libs based on its concepts.
Based on my experience with YUI, their Y.app will have the most consistent and complete api set and the best docs.
If we take your specific case, I would strongly go for Knockout.js
Knockout is built for UI i.e. that your data model can be binded to HTML elements and then any changes in the model manipulate the UI accordingly.
In your case the additions/deletions to the contact list will be automatically tackled by Knockout. All you need would be to define the how the changes in the tree view are to be tackled (either by code or by HTML templates) and Knockout will take care of that.
Saying that, defining the treeview of your contact list is still different task altogether and you may need to you use some CSS/JS library for that.

Implementing 1-page JavaScript web app using the principles of MVC

How would you implement this app in JavaScript using principles of MVC?
As you can see, the app basically has 3 views: "Sidebar", "Visualization" & "Timeslider". The underlying model must keep track of selected countries and year.
When initing the app, it should load a json file that populates the model with data. With some sort of event system (please advice), the controller should be notified of a stable model, and corresponding views should be set.
For example, adding a country in the sidebar should trigger data reload, followed by updating of x/y scales of the visualization view to accomodate for more countries etc.
How would you separate the logic from the views (what would go in the model, view & controller respectively) and what libraries would you use? (especially re. event handling).
Code is much appreciated ... Thanks.
For this kind of application I suggest you to look into backbone.js.
Which basically is:
Backbone supplies structure to
JavaScript-heavy applications by
providing models with key-value
binding and custom events, collections
with a rich API of enumerable
functions, views with declarative
event handling, and connects it all to
your existing application over a
RESTful JSON interface.
You can take a look at Views on the backbone.js documentation; they have a huge amount of examples for you available, which will be more helpful than me providing a complete solution using backbone.js. They also have several demos with fully built apps ready fo you to base off.
Update: Also take a look into spine.js which is really similar to backbone but it's not the same, from their F.A.Q the difference is:
Whoah - your API looks really similar to Backbone. Why should I use this Instead?
Well, it's true that Spine
was inspired by Backbone, an excellent
library, and its controller API is
very similar. However, the
similarities end there. Internally the
library works very differently. For
example, Spine has no need for
Collections, which are required for
pretty much every model in Backbone.
Spine provides a class library, and
has very different ideas when it comes
to server sync. Lastly, Spine is much
simpler and half the size, go and
check out the source.

Any dynamic database frontend tool from which you can update directly?

This is with reference to this question where I got one tabular report format.
I need to update the user entered values correctly back to the same table rows. I am in the process of doing this by using general form post data methods by using some logics which I think will not be easy to maintain. So, just out of curiosity, Is there a front end creator javascript libraries or frameworks which can create the front end for any query's resultset and updates the corresponding rows when the user updates them from front end. This need not have all the full functionality, any one usable, customizable thing will reduce the code maintenance problems. I have googled for some javascript libraris for this but not able to get which will be suitable. Please suggest any useful tools. My environment is Mysql, PHP, JQuery, XAMPP server on Windows. Is JQuery provides one.
Thanks in advance.
"...from any query's resultset..." I suspect there is no tool that can do that. Imagine a resultset of data from a table with one (or more) columns which has a foreign key to another table(s). A tool would need to find the relationship between the tables, update the "lookup table", and then update the dependent table. Now imagine a more complicated resultset from a variety of tables - maybe some of those tables are views. Tall order...
I use phpMyAdmin to update tables in my schema, but I perform these updates as an admin rather than a user.
I dont know the nature of the app nor do i know of any JS libraries with this type of functionality. I think i would look at PHP libraries/components as opposed to JS. For example Symfony could lend you this functionality through the use of its admin generator features. While converting something youre well along on to Symfony for the sake of admin generation probably isnt the best idea, i think youre bound to find alo tmore resources on the server side than the client side. :-)

Categories