What's the appropriate granularity for Backbone.js Views? - javascript

I'm adopting Backbone.js to render a small corner of an existing large web app. If this goes well, I can see Backbone.js growing to encompass the whole of the app, lending some much-needed structure to an organically-grown application. That's the preface. Now for the problem:
I have a select box which allows the user to select a reading plan. When the selection changes, the view updates some descriptive text, a calendar interface, and a little widget for marking today's readings as complete. The widget will have a checkbox for each reading (one or more) in today's entry and a button for continuing to the next day's reading. (You can see the current, non-Backbone version of this interface (minus the completion scheme) on the right-hand side of the existing app.
What is the appropriate granularity for each View? I've identified the following "fiddly bits":
The Tab itself, which encompasses all the contained controls.
The select box
The descriptive text, which responds to the select box
The calendar, which responds to the select box
The readings widget, which responds to the select box, and contains:
Optionally, a "Start" button, which activates the current plan.
When activated, one or more checkboxes corresponding to individual readings within today's entry.
When activated, a "Next" button which completes today's entry and displays the next.
Should each of these bullet points get its own View? Just the major pieces (tab, select box, widget)? The first will result in quite a few Views. The first seems like it could lead to overcomplicated View implementations. What's best?
Note: I realize this could be interpreted as a wildly-subjective question, but I'm still wrapping my head around Backbone.js and Javascript/DOM MVC patterns, and I'm hoping that there is a narrow "this is what's intended/works best" from more experienced Backbone.js practitioners. Thanks!

In general, the granularity of your views will depend on finding a balance between the complexity of the particular piece of UI, and over-fragmentation of views into little pieces. I probably would not use a view for something as small as a button (a CSS class is all you really need for that).
In your particular case, I'd probably have a view for the calendar widget -- so it can be easily reused in other places in the app -- and a view for the entire Devotions tab. The rest can be done through event binding.
Regarding model updates and re-rendering, the whole idea with Backbone is to separate that concern from the views. Models emit "change" events when their attributes change, and whatever views happen to be on the page at the time, and are displaying that particular model's data, will be notified of the change, and can update themselves.

There is no definitive answer to your question. You can have a look at the granularity of sproutcore for exemples.
You can also watch http://vimeo.com/17186379 where Yehuda Katz illustrate the difficulties of updating different pieces of the page.
One way of looking at it would be to check which part should be refreshed with different model change/events and try to minimize the re rendering.
Sorry no good answers as you pointed out ;)

Related

Ext JS 4.2 primer

My company just bought a third party application which is based on the ext js 4.2. framework.
The software is closed source, but it is web based such that I can add a .js file to change the UI to my needs.
I want to add some controls to the rendered page. The software is showing IDs everywhere instead of text.
Example: "Issue created by: ID123". When I hover the field is get "ID123. John Doe". Ok, I am a JS ninja, so I can just add a field to the HTML DOM which will display "John Doe"in the correct spot.
I looked at the HTML code to get the correct control and see the the IDs are generated. The code I would write is prone to break with each new release of the third party software.
Now, since the is an Ext JS application I can probably solve the issue much more elegantly like adding a field not to the DOM directly but to the Ext JS container.
Question:
In Javascript I have a reference to the Ext JS app. How would I access the current view or viewmodel or model to query data and add a field?
Rough idea/Pseudo code:
var id = app.getCurrentModel.getValue("creatorID");
var name = myserver.getPersonData(id).name;
app.currentView.addLabelControl(name);
I googled a lot but all examples I found assumed that you are writing the ext js app and you are already in the controller or the view. But I only got the reference to the app.
Sorry for the newbie question :)
"App Inspector for Sencha"
For a quick glance over component hierarchy, you can use the Sencha browser plugin,
Find a certain ExtJS component programmatically
To quickly search ExtJS components or transform your findings into code, your main tool will be the browser console and the command Ext.ComponentQuery.query(xtype), e.g.
Ext.ComponentQuery.query("grid")
Ext.ComponentQuery.query("panel")
Ext.ComponentQuery.query("form")
You will then find in browser console an array of all components of that type. Select the right one, and check whether it has an id or itemId that is not auto-generated (everything like xtype-1234 is auto-generated). For form fields, the name attribute could be useful. Commands like
Ext.ComponentQuery.query("[itemId=ABC]")
Ext.ComponentQuery.query("[name=DEF]")
Ext.getCmp(id)
are far more readable and not as prone to side effects as Ext.ComponentQuery.query("panel")[12].
Most of the time, it can also be useful to think in tree structure. If you want a certain container which contains the only slider you see, trying
Ext.ComponentQuery.query("slider")
Ext.ComponentQuery.query("slider")[0].up()
could be easier than sifting through dozens or even hundreds of containers. Ways to traverse the component structure include up(xtype), down(xtype), nextSibling(xtype), previousSibling(xtype). If an xtype is provided, the next component of the corresponding xtype is selected; if it isn't provided, the next component is selected regardless of the type (e.g. direct parent, adjacent sibling).
Change anything you want.
You can extend, debug or modify any existing behaviour, including but not limited to ExtJS's own code, using a so-called override over any component, including the views or stores that make up this app. override makes a great search term for further information.
Or you can add new components to existing components, like a button to an existing form, from outside the app. For example, open sencha docs and then insert in console:
Ext.ComponentQuery.query("searchcontainer")[0].up().insert(1,{xtype:'button',text:'Test',handler:function(){Ext.Msg.alert('Test Button clicked');}});
You should then find a button on the top left, right of the Sencha logo. Click it.
Find existing controllers
For this, you have to find the name of the app namespace.
If it is e.g. MyApp, then MyApp.app.controllers.items contains the list of controllers. Controllers contain control logic, and the mapping between the components and the logic. When components are created, controllers attach their events to these new components. Many changes can and should be made in the component layer, because controller overrides are messy.
Find viewmodels
You're already done, ExtJS 4.2 does not support them.
Changing models
If you want to change models, be cautious: There is no supported function to add fields to a model. You can override the model prototype, and push more entries into the fields array. But if you have any model instances (records) already running around by that time, they are not updated and any existing warranty is voided.
That said, you find them in MyApp.model. You can e.g. get all fields of the Sencha Docs' Comment model using Docs.model.Comment.prototype.fields, or even push another field in.

Hierarchy table - activation of rows through javascript?

I've created a database-driven hierarchy table with 3 hierarchy levels. It works almost exactly how I want it to, but the javascript code that drives its functionality is UGLY. I would like to re-write some of it, but I don't quite know how. I imagine I'll have to use recursive functions, but there are some strange business rules that may make it more difficult than I imagined. Can you make any suggestions to simplify my current code (in particular, the ActivateRow() function)? See fiddle.
Note that there is some funky use of hidden fields due to ASP.NET Web Forms postbacks and losing input values, but there are some useful data-attributes written into the HTML such as data-region-id, data-subregion-id, data-market-id, and data-hierarchy-id. the main requirements are below:
There are two tables - the first is linked to the second, so all actions on the first table should be carried out on the second table as well (though the second table is slightly different).
Three levels of hierarchy. The highest level (Region), a middle level (Sub Region), and a final level (Market)
Row activation enables all controls in a given row. If the activated row is a Sub Region row, it deactivates its parent Region row controls and its sales values sum up to the Region level. This does not apply to activating a Market row.
Similarly, row deactivation should disable and clear all controls within that row. If the row is a parent row, it should clear all children rows and disable their controls as well.
Deactivating all children of a parent row should deactivate the parent row and clear all parent row controls.
Surely, there's a better alternative. Am I reinventing the wheel? Is there anything out there that I could use to improve my spaghetti code? I realize this isn't the best question for Q&A format - is there somewhere else I should ask my question?
It is not entirely clear what you are doing but from what I gather you have some kind of table you are filtering.
Also, you are using ASP.NET webforms.... so sorry for you.
I would not use any kind of form binding controls offered by ASP WebForms, because it is now obsolete, incorrect and very painful.
Instead, create an AJAX enabled page [WebMethod] that returns all the data or filtered with a parameter, what ever, in JSON (Because JSON is easier to read and debug, and much faster than XML)
Go and find the jQuery plugin DataTables. Everykind of functionality is built in and extentable, it works great, super efficient client side, with varios filtering and searching capabilities.
You either create a new model on the Server side and remap the data server side, or dump your existing model and remap it on the client, either way, you just plug JSON data into datatables.
Saving back to webforms is slightly more tricky (compared to MVC/Razor) because you have to take the String and deserialise it to your model, its a few extra lines but I do this all the time and you can carry on with your business logic.

Auto update table fields and disabled function using javaScript, Ruby on Rails and/or JQuery

Versions
Mac OS: OSX 10.10.2
Ruby: 2.2.1p85
Rails: 4.2.0
Bootstrap-SASS: 3.3.4 #For Web UI
Bootstrap-Switch-Rails: 3.0.0 #For Toggles
Context
I am building a site where someone can create a packing event to make bags for the homeless. I am using Ruby on Rails to create this webapp, Bootstrap to design the user interface, and am open to using JQuery, javaScript, HTML, and CSS to get the desired functionality. Though my working knowledge of the first two languages is minimal.
In the user's event creation and edit pages I have a list of items that a person can add to their bags. This list is in a table view as seen through the link below.
Dropbox link
Desired Functionality
I am wanting to do 2 things.
When the user enters the number of bags they plan on packing, I want to be able to update the column "Number Needed" for each item. So, for instance in the example scenario shown in the image above, the "Number Needed" for "Raise Money" would be "$6,500", for "Volunteers" would not be added, for "Water" would be "1000" and for "Food" would be "1000".
I want the user to be able to toggle whether to include an item on their event page. So, in the example picture, the user wants to show on their event page that they are raising money for their event, and requesting donations for water and food; but they do not need volunteers to pack the bags. In this scenario, I would like to disable the form fields of "Number Needed", "Currently Have" and "Number Remaining to Get" for the row "Volunteers".
Questions
For the first desired functionality specified above: Should I just have the table on a separate page maybe so that the fields are populated before the table is actually shown? Or, my preferred ability, do I need to put a button after the "Number of Bags" field that when clicked updates the table? And how would I do that?
For the second desired functionality specified above: Is there a way to have this done dynamically when the user toggles the "Include" column to "Yes" or "No"? How would I disable the desired form fields dynamically once a user toggles that row to "No"?
Let me know if you need any more information. And thank you in advance for your expertise.
First things first, I would put this in the comment, but I don't have enough reputation to do that yet.
Ok so there's a few ways to solve your problem. The first and ugly solution would be to rerender the page everytime something gets updated. You really shouldn't do it that way because that required the user to hit a button to submit the form and the controller would render the same page again with updated results.
Instead, you want to use jQuery to add event listeners to your form fields and the button mentioned in the second functionality. On those event listeners, put some handlers to update the desired field. It's hard to direct you on the jQuery if I don't know your HTML structure. As Santiago mentioned, there are many tutorials out on the internet for putting event listeners and creating the proper handlers.
Additionally, you should consider AJAX to asynchronously communicate with your controller to keep the front-end (what the users see on the browser) and the back-end (data stored in the database) in sync. Again, plenty of guides out there on the internet for this, but definitely look into the Rails functionality of "remote: true" on your form to make the set up of the AJAX process easier.

How do I build a multi-stage web form?

I'm trying to build a webform that has multiple stages. I'm patterning it off of the Stack Overflow / Stack Exchange flagging webform. The problem is, I'm not sure how to trigger the "next stage" action.
To illustrate, if one wants to flag a question on Stack Overflow, you click flag and then a popup prompts you to make a choice. If you choose the second option ("it doesn't belong here, or it is a duplicate") the form automagically takes you to a second screen.
First screen:
Upon click, it auto-redirects to:
The problem is that I don't know what the underlying trigger is. How does clicking that radio button send the end user to the next screen?
I tried checking the source, but I have a feeling I'm only seeing half the picture:
No amount of HTML tutorials I find have any practice example similar to this. I suspect this is JavaScript, but I can't find the linked .js file that would trigger these actions.
So: How does the webform auto-redirect upon click? As a follow-up, if it's using JavaScript, is there an HTML/CSS-only workaround I can use?
It might help to think about this at a lower level than frameworks. There are two ways one could make a multi-stage form.
The first (and generally older) way is to store the state on the server. Each stage of the form is actually a separate form, and the client's progress through the questionnaire is kept on the server (for example, as part of the session data).
The second way (the more modern one) is to use JavaScript, as you suspected. There is actually very little black magic in this case, and no auto-redirects on clicks at all. All you do is have one very long form where you show/hide some of the elements depending on the user's selections (of course, you could have multiple <form> elements which you show/hide).
Well, I'd use some sort of jQuery wizard plugin and adapt it to my needs. I did it very recently and it wasn't that hard. You should try SmartWizard 3.0, it's pretty active, the last release was about 2 months ago and the coders answered my questions pretty fast.
Here it is: https://github.com/mstratman/jQuery-Smart-Wizard
You can trigger the wizard to advance to the next step linking an event to the action of clicking the radio button.
Good luck :)

Accessible web datagrid?

I'm building a budget webapp, mostly for my personal needs and for the sake of self training. I may release it later at some point.
The interface will feature a table of operations (credit / debit). I was planning to use Ajax to make the table "editable" by clicking in a cell (Excel-like).
I therefore need to:
diplay operations
add new ones
modify existing ones
I fail to see how to make "modify" degrade nicely, as if you remove JS, this will be a plain old table without the possibility to modify an existing entry.
Turning the table into a giant form would be ugly, adding links to edit each operation then hide them using JS seems fairly complex...
One possible solution would be to add links that go to other forms that allow the modify operations to take place. This is much less fluid than in-place edits for accounts, but it still remains accessible without being burdensome (like a huge page of form inputs would be).
You can then override the links with javascript to give the ajaxy, web-app functionality you are looking for.

Categories