Multimple forms for one model backbone.js - javascript

The question is about managing models in forms on client with backbone.js
There is a model User on the server. It contains various fields:
Personal info
Name
Last name
Date of birth
Other info
About me (text)
My hobbies (text)
I have a single page application of settings. There are two forms on it: "personal info" form and "other info" form. I although have an api, that contains two routes to handle it - /api/user/<id>/personal_info, /api/user/<id>/other_info (it could be changed, it does not matter). I can PUT or GET info from these apis.
So I can't decide how to organize my backbone models right. Right now I have two models - UserPersonalModel and UserOtherModel, each of them has it's own api and I save them apart from each other.
Am I doing right or I shall rewrite it to one js model UserModel and call different save methods like .savePersonal and .saveOther? What is the best practice?
This question came here from ru.stackowerflow.com

Unless you're displaying multiple user forms at the same time and you'll have to do extra work mapping each other_info model with respective personal_info, I don't see any reason to combine those two as single model.
Especially since you have two endpoints, it's easier to have them as separate models, you can have separate view controlling each forms with respective models as well.
Combining the models will probably create nested attributes, then you'll have to manage that as well (Using plugins like deep model).

Related

Using MVC pattern for JS app with multiple models

I'm trying to comprehend usage of MVC pattern for JS application in case of working with multiple models. For example, I have a page with user info, his orders and executors of order. I need to show some user info and make orders and executors editing. So I have 3 models and need controller and view for each. Besides orders and executors are in lists. All lists for consecutive use in models must be loaded from server. What approaches should I use to manage multiple MVC entities in one page? Maybe another design patterns, etc.
UPDATE
To clarify
But, if I need to make a separate page where I work only with orders or only with executors. It is logical that these should be separate entities so that they can be used in various tasks. I thought that, in this case, perhaps, it is necessary to assemble one "large" model from the "small" models to solve a specific problem, or to make some kind of interface for interaction between models.
What am I talking about? For example, when I load orders from the server, I also load some information about the executor, i.e. in a SQL database, this is done by some JOIN of two tables. Which of the models to make a request for loading? Obviously orders. How, then, initialize the executor model data? Okay, I'm doing one big model where are all data I need about all of the entities in my application.
Ok, the page (modal, whatever) for executor's editing. There is no need for order data or customer (user) data. For this I use the same model, where some of the data and functions associated with them will not be used. Something is not sticking with me :)
Just a correction, MVC is an architectural pattern and not a design pattern. However the 2 differ by a thin line so its very easy to mistakely interchange both during discussions.
Irrespective, MVC should be a good approach to structure your code:
- authentication [Feature1]
- pages
- signin_page.htm
- authentication.css
- signup_page.htm
- models
- user_info.js
- signin_info.js
- auth_controller.js
- orders [Feature2]
- pages
- models
- order_info.js
- payment_info.js
- order_controller.js
- Feature3
- Feature4
- shared [all common things]
- services
- http_service.js
- models
- error_info.js
Basically you can create a folder for each feature or functionality and then inside that on a need basis create folders namely pages/views and models and then 1 or more controllers for each feature and a single generic service to do http calls which should take parameters and url as input and should handle all the error handling.

How to have multiple entries for custom entity?

In our Dynamics CRM online custom project - we've the default ACTIVITIES tab in a custom entity named DocProject's form
ACTIVITIES is able to take multiple entries.
Also, there is NOTES tab in the same form
NOTES is also able to take multiple entries.
Okey, this is done by Dynamics CRM guys. So far so good.
In the same form, we also have a DocProjectActivities lookup field for a custom entity DocProjectActivities
This is a lookup field, hence it has got a 1:N relationship.
Clarification: Our problem is not only about Activities. WKT Notes also behaves similarly. We just need some config which will allow us to make multiple entries for one single field
Problem:
How do one makes sure that this custom entity DocProjectActivities allows to make multiple entries as that for ACTIVITIES & NOTES?
In order to have the associated activity grid like the one in the DocProject entity, you need to enable the option Activities when you're creating the entity (this option can't be changed after the entity is created). This option will create the association with the activities entities and allow you to track all the related phone calls, task, etc.
I think that the problem that you're having is that you defined the DocProjectActivities as an activity entity and therefore you can't have this kind of relationship with the other activities entities. I recommend you to take a look to the differences between Entities and Activity Entities.
Do you mean you need multiple docprojectactivities on the form where you have the activities?
You need to create a 1:n relationship between the form and the docprojectactivities and add the subgrid on that form allowing you to create multiple records for the docprojectactivities.
A lookup field is the '1' side of the 1:n relationship and thus the wrong direction.
Go to docprojectactivities, add a new field type relationship towards the entity you are working on.
Save and publish.
Now go back to the form designer of the entity you need the entries on and go to the tab 'insert'.
Click on sub-grid and select only related records docprojectactivities (entity you are working on)
If you want your custom entity to work as an activity entity, you should have selected "Define as an activity entity" when you created it.
You cannot make a normal lookup field to multiple entities (with the exception being the possibility of creating Customer fields that was introduced in 2016.1).

Spring-mvc + Thymeleaf: dealing with complex form

I'm working on an internal tool using spring-mvc and thymeleaf.
A section of this tool is used to create an entity we save in the database.
This entity is quite complex; it contains many properties and relations. Some of these relations contain list and other properties.
I have 2 constraints:
Single page. No "wizard".
To only save a completed object in the database.
Now, I'm not really asking for a specific issue. I know my way around thymeleaf, spring #ModelAttribute, etc.
My question is mostly which strategy are you choosing or how to deal with really complex object creation.
Now I can see 3 ways to do it :
Rendering page with thymeleaf. Every time a new element need to be added to a list, I use Ajax to add the new element on the server and rerender the specific fragment. So doing back and forth to the server with my #ModelAttribute and only save at the end.
Rendering a basic page with thymeleaf. Using JavaScript to create html elements and instead of submitting to a #ModelAttribute, I'm serializing my form to JSON and submit this JSON to the server. (kind of client side model)
Rendering a basic page with thymeleaf. Create the html element dynamically with JavaScript when I need to add list item (being sure I'm putting proper name="" to fit with my Java form object) and submit the whole thing at the end.
I'm personally unsure between 1 or 2.
I feel dealing with complex object is much more easier using JSON than form submission. Also, the input value/field with sub object and property can be quite nasty. Having this kind of syntax
does not sound great to me...
3 can probably work but the way spring data binding is done with sub property is lacking some detail in my humble opinion (section 7.4.1 - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html).
What do you think ?
Personally I use Thymeleaf's own dynamic field management to ensure clean addition of objects and fields to object.
So I will recommend option 4: Dynamic Field management by Thymeleaf.
Have a read of http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields.
I use that for both single field additions as well as addition of nested forms. Does the trick no questions asked.
Hope that helps.

Multiple AJAX requests to populate form fields?

Let's say my Backbone.js-based web application has a form containing multiple drop downs, each containing a different type of data, populated via API data.
As I am using Backbone.js, my application logic lives entirely on the client side. Thus, I do not want to populate these drop downs via a typical server-side MVC approach of injecting data into the MVC view via the server side; instead, I want Backbone to retrieve data for these dropdowns.
So, my question is: To populate three different dropdowns, will I perform three different AJAX requests to my API? Example:
GET /categories/
GET /countries/
GET /vehicle/models
Or does it make sense from a RESTful perspective to combine these into some "meta" API method?
I think Backbone is pretty agnostic about this. If you don't mind making three requests, it's certainly okay to do so. If you want to make one call to your API that compiles all three groups of data together and returns them, to be processed on return that is totally legitimate as well.

Controller and View for creating one-to-many object, both "container" and unlimited number of "content" objects?

Users will be able to write some documents. Those documents will consists of chapters (one-to-many relation).
Normally I would do this by creating separate views for creating chapter and document.
How to implement web page that allow to edit "composite" view? Where I can edit document details, but also create chapters, without visiting different pages? Also how can I ensure that I pass order of chapter user have arranged (by moving chapters freely up and down)?
(Sorry if that question already have be asked&answered but I do not even know how to search for it :| since I do not know proper keywords beyond "AJAX", so help in naming my requirement would also be welcomed!)
Backend servers applications based on REST principles work nicely with Ajax client-side implementations.
For example, your URLs could be:
/book/1
/book/1/chapters
/book/1/chapter/1
You could set it up so that a POST to /book/1/chapters would add a chapter. A GET on that same URL would return all chapters. A GET on /book/1/chapter/1/ would only return chapter 1. A PUT on /book/1/chapter/1/ would update an existing chapter. This is a "RESTful" architecture:
http://en.wikipedia.org/wiki/Representational_state_transfer
This is an interesting introduction: http://tomayko.com/writings/rest-to-my-wife
This is a big subject, but if you create the right backend server architecture you will find your job a lot easier. Hope this helps answer your question.
Ok Partial solution.
Just google Nested Forms Ruby on Rails. Plenty of examples, all in ajax, all easy.

Categories