-
Notifications
You must be signed in to change notification settings - Fork 1
employee filter.asc.desc.load #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
ex9/phonebook.html
Outdated
| <body> | ||
| <input type="button" id="btnReloadList" value="Reload" /> | ||
| <input type="button" id="btnASC" value="ASC" /> | ||
| <input type="button" id="btnDESC" value="DESC" hidden /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know there's a hidden attribute. But it seems fine.
ex9/phonebook.html
Outdated
| <input type="button" id="btnDESC" value="DESC" hidden /> | ||
|
|
||
| <input type="button" id="btnSearchOn" value="Search" /> | ||
| <input type="text" id="textSearch" value="" hidden /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would put all the search stuff into some container (<div>) so that I can hide it all in one action. It would also help to see the high-level features in your page.
You can then also give the container an ID/class (let's say "search") and rename the inner elements' ID, or might not need to give them IDs/classes at all as they are the only ones in this container (#search input { ... }).
ex9/phonebook.html
Outdated
|
|
||
| <ol id="phoneBook"> | ||
| <li> | ||
| <span class="name fname"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that you actually load the data dynamically, I'd remove the pre-filled values.
ex9/js/main.js
Outdated
| } | ||
| }, | ||
| searchChange: function() { | ||
| console.log('searchChange'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This repeats exactly what's above. It normally implies you need to refactor the code so there's no repetition.
ex9/js/main.js
Outdated
| searchOn: function() { | ||
| console.log('searchOn'); | ||
| // Search off | ||
| $( "#btnFilter" ).prop( "disabled", true ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would call searchChange instead of setting disabled directly.
Also, since you are not clearing the input after you hide and re-show it, it probably starts with the last search then, but the button is disabled.
ex9/js/main.js
Outdated
| var on; | ||
| setEvents(); | ||
| on = { | ||
| loadList: function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is WAY too big and deal with three different things. Please split these into three files:
- searchui
- sortui
- data (which will later also include the code to do the searching/sorting).
This will also make you think about what values you need from #1 and #2, and will make them more like objects.
| @@ -0,0 +1,95 @@ | |||
| require(['ajaxGet'], function(ajaxGet) { | |||
| $(function() { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would actually inline (=move the code of something into here) setEvents into the $(function() {...}), but move the on outside. Also, I don't think the on object helps in any way, I'd vote to just its methods be top-level functions.
ex9/js/order.js
Outdated
| @@ -0,0 +1,27 @@ | |||
| define(function() { | |||
| return function() { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just return setEvents; instead of this anonymous function
ex9/js/search.js
Outdated
|
|
||
| function showSearch() { | ||
| console.log('searchOn'); | ||
| $( '#btnFilter' ).prop( "disabled", true ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would call onFilterChange instead.
ex9/js/search.js
Outdated
| function showSearch() { | ||
| console.log('searchOn'); | ||
| $( '#btnFilter' ).prop( "disabled", true ); | ||
| $('#filter').val('Type your filter'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ex9/js/ajaxGet.js
Outdated
| url: url, | ||
| statusCode: { | ||
| 404: function() { | ||
| alert( "page not found" ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't do this error specific approach. There are MANY errors that can occur. The error callback is enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
ex9/js/ajaxGet.js
Outdated
| }, | ||
| error: function(fff){ | ||
| alert( "it is an error" + fff ); | ||
| console.log(fff); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once you understand what is the argument, rename fff to it. Also, the doc for error says what this param is. See http://api.jquery.com/jquery.ajax/
ex9/js/main.js
Outdated
| @@ -0,0 +1,37 @@ | |||
| requirejs(['ajaxGet','load','search', 'order'] , function(ajaxGet, load, search, order) { | |||
| $(function() { | |||
| var fun = { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, you don't need this object for calling methods on. If you want this to hold the data, just rename it to data (or better, phoneBookData) and access it from the functions as needed.
For the methods that others (order, filter) need to call, you can just pass another object: return {sortByAsc: ...}
ex9/js/main.js
Outdated
| }, | ||
| filter: function() { | ||
| var filterList = _.filter(fun.data, function(name) { | ||
| if (name.name.indexOf($( '#filter' ).val()) > -1) return name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filter expects your function to return true or false (http://underscorejs.org/#filter). You currently return name or undefined instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tnks
done
ex9/js/main.js
Outdated
| ); | ||
| fun.print( _.sortBy(filterList, 'name').reverse()); | ||
| }, | ||
| print: function(objList) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print is usually for something like console.log. Use render instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
ex9/js/filter.js
Outdated
| }); | ||
|
|
||
| $('#filter').on('keydown', function(val) { | ||
| if (val.which == 13) filter(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if you press Enter but there's no input (val==='' as below)?
That's why you need all entry point to search to use the same code?
ex9/js/main.js
Outdated
| }; | ||
|
|
||
| load(function() { | ||
| ajaxGet('json/data.json', function(phoneBookDataA) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
phoneBookDataA is not too descriptive. How about phoneBookDataFromServer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
| requirejs(['ajaxGet','load','filter', 'sort'] , function(ajaxGet, load, filter, sort) { | ||
| $(function() { | ||
| var phoneBookData = {}; | ||
| var render = function(objList) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var render = function(objList) is almost the same as function render(objList), except the latter cannot be modified. Use the latter form as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please put some comments as to what objList is. Similar to phoneBookData, it is an array of employees (technically a subset of phoneBookData).
ex9/js/main.js
Outdated
| @@ -0,0 +1,38 @@ | |||
| requirejs(['ajaxGet','load','filter', 'sort'] , function(ajaxGet, load, filter, sort) { | |||
| $(function() { | |||
| var phoneBookData = {}; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think your code elsewhere expects to find an array and not an object (try filtering or ordering without loading the data -- you'll probably get an Error). Initialize to [] instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
ex9/js/main.js
Outdated
|
|
||
| filter(function() { | ||
| var filterList = _.filter(phoneBookData, function(name) { | ||
| if (name.name.indexOf($( '#filter' ).val()) > -1) return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of here accessing the "internal" of the filter UI, have the caller (in filter.js) pass the filter value into this the function filter calls back (line 25 above).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
ex9/js/main.js
Outdated
| render( _.sortBy(filterList, 'name')); | ||
| }); | ||
|
|
||
| sort(function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the sortAsc and sortDesc are similar, I'd make them use the same callback with some param passed (for example: sort({asc: true}); or sort({asc: false});.
This makes even more sense when there's more info you wish to convey to the callback, like in filter (jQuery use objects for almost everything, think $.ajax).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
ex9/js/filter.js
Outdated
| }); | ||
|
|
||
| $('#btnMin').click(function() { | ||
| console.log('searchOff'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clicking on this should undo the filter. Currently it only affects the filter UI.
| @@ -0,0 +1,17 @@ | |||
| define(function() { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file isn't being used anymore. Delete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rm order.js==>sort.js search.js==>filter.js
done
No description provided.