Skip to content

Conversation

@zyahav
Copy link
Contributor

@zyahav zyahav commented Jan 4, 2017

No description provided.

<body>
<input type="button" id="btnReloadList" value="Reload" />
<input type="button" id="btnASC" value="ASC" />
<input type="button" id="btnDESC" value="DESC" hidden />
Copy link
Owner

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.

<input type="button" id="btnDESC" value="DESC" hidden />

<input type="button" id="btnSearchOn" value="Search" />
<input type="text" id="textSearch" value="" hidden />
Copy link
Owner

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 { ... }).


<ol id="phoneBook">
<li>
<span class="name fname">
Copy link
Owner

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');
Copy link
Owner

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 );
Copy link
Owner

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() {
Copy link
Owner

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:

  1. searchui
  2. sortui
  3. 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() {
Copy link
Owner

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() {
Copy link
Owner

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 );
Copy link
Owner

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');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url: url,
statusCode: {
404: function() {
alert( "page not found" );
Copy link
Owner

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

},
error: function(fff){
alert( "it is an error" + fff );
console.log(fff);
Copy link
Owner

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 = {
Copy link
Owner

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;
Copy link
Owner

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.

Copy link
Contributor Author

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) {
Copy link
Owner

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.

Copy link
Contributor Author

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();
Copy link
Owner

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) {
Copy link
Owner

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?

Copy link
Contributor Author

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) {
Copy link
Owner

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.

Copy link
Owner

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 = {};
Copy link
Owner

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.

Copy link
Contributor Author

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;
Copy link
Owner

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).

Copy link
Contributor Author

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() {
Copy link
Owner

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).

Copy link
Contributor Author

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');
Copy link
Owner

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() {
Copy link
Owner

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.

Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants