Skip to content

Commit

Permalink
- Added listingView Scene. A way to view listings found in search res…
Browse files Browse the repository at this point in the history
…ults.

- Clean up the search function majorly. We're still learning here folks. Well, I am. Now complete with results number and error checking.
  • Loading branch information
lozzd committed Apr 18, 2011
1 parent cdd019e commit 569b620
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 39 deletions.
54 changes: 54 additions & 0 deletions app/assistants/listingView-assistant.js
@@ -0,0 +1,54 @@
function ListingViewAssistant(tappeditem) {
/* this is the creator function for your scene assistant object. It will be passed all the
additional parameters (after the scene name) that were passed to pushScene. The reference
to the scene controller (this.controller) has not be established yet, so any initialization
that needs the scene controller should be done in the setup function below. */
gListingdata = tappeditem;
}

ListingViewAssistant.prototype.setup = function() {
/* this function is for setup tasks that have to happen when the scene is first created */
this.controller.get("listingName").update(gListingdata.listingTitle);
this.controller.get("listingDesc").update(gListingdata.description);
this.getImageURLs(gListingdata.listing_id);
/* use Mojo.View.render to render view templates and add them to the scene, if needed */

/* setup widgets here */

/* add event handlers to listen to events from widgets */
};

ListingViewAssistant.prototype.activate = function(event) {
/* put in event handlers here that should only be in effect when this scene is active. For
example, key handlers that are observing the document */
};

ListingViewAssistant.prototype.deactivate = function(event) {
/* remove any event handlers you added in activate and do any other cleanup that should happen before
this scene is popped or another scene is pushed on top */
};

ListingViewAssistant.prototype.cleanup = function(event) {
/* this function should do any cleanup needed before the scene is destroyed as
a result of being popped off the scene stack */
};

ListingViewAssistant.prototype.getImageURLs = function(listingid) {
// TODO: Let's make this a ImageViewer! Those sound fun.
var url = apibase + 'public/listings/' +listingid + '/images?api_key=' + apikey;
var request = new Ajax.Request(url, {
method: 'get',
onSuccess: function(transport){
var data = transport.responseText.evalJSON();
if (data.results != null) {
this.listingimageurl = data.results[0].url_170x135;
this.imagesrc = '<img src="' + this.listingimageurl + '">';
}
this.controller.get("listingImage").update(this.imagesrc);
}.bind(this),
onFailure: function(){
Mojo.Log.error('Failed to get response from Etsy API');
}
});
};

54 changes: 16 additions & 38 deletions app/assistants/search-assistant.js
Expand Up @@ -28,14 +28,15 @@ SearchAssistant.prototype.setup = function() {
items: [ ]
}
);

/* add event handlers to listen to events from widgets */
this.viewListingHandler = this.viewListing.bindAsEventListener(this);
this.controller.listen("searchResultList", Mojo.Event.listTap, this.viewListingHandler);
};

SearchAssistant.prototype.activate = function(event) {
/* put in event handlers here that should only be in effect when this scene is active. For
example, key handlers that are observing the document */
this.controller.get("searchDescText").update("Search results for " + global_searchterm);
// SearchAssistant.prototype.getSearchResults(this.searchterm);
};

SearchAssistant.prototype.deactivate = function(event) {
Expand All @@ -52,63 +53,40 @@ SearchAssistant.prototype.cleanup = function(event) {

SearchAssistant.prototype.getSearchResults = function(listWidget, offset, limit) {
var url = apibase + 'public/listings/active/?api_key=' + apikey + '&limit=20&keywords=' + global_searchterm;
try {
if(!global_searchterm) {
throw('No search term entered');
}
var request = new Ajax.Request(url, {
method: 'get',
onSuccess: function(transport){
var data = transport.responseText.evalJSON();
if (data.results != null) {
var listings = data.results;
var items = [];
for (var i = 0; i<listings.length; i++) {
mylisting = listings[i];
item = {};
item.listingTitle = mylisting.title;
item.listingPrice = mylisting.currency_code + ' ' + mylisting.price;
item.listing_id = mylisting.listing_id;
item.description = mylisting.description;
item.state = mylisting.state
item.views = mylisting.views
item.favouriters = mylisting.num_favourers
items[i] = item;
}
listWidget.mojo.noticeUpdatedItems(offset, items);
},
}
this.controller.get("searchDescText").update("Showing " + this.controller.get("searchResultList").mojo.getLength() + " results for " + global_searchterm);
}.bind(this),
onFailure: function(){
Mojo.Log.error('Failed to get response from Etsy API');
this.controller.get("searchDescText").update("Search failed, please try again later");
}
});
}
catch(e) {
Mojo.Log.error(e);
}
}

//SearchAssistant.prototype.processSearchResults = function(response) {
// Mojo.Log.error('entered process results');
// var datajson = response.responseJSON;
// if(datajson.results.length=0) {
// this.controller.get("searchDescText").update("No results for " + global_searchterm);
// Mojo.Log.error('No search results');
// }
// Mojo.Log.error('test is ' + datajson.results[1].listing_id);
// var listings = datajson.results;
// Mojo.Log.error('listings is ' + listings[1].listing_id);
// var items;
// try {
// for(i=0; i<listings.length; i++) {
// Mojo.Log.error('Listing:' + thislisting.listing_id);
// thislisting = listings[i];
// item = {};
// item.title = thislisting.title;
// item.description = thislisting.description;
// item.price = thislisting.price;
// item.currency = thislisting.currency_code;
// items[i] = item;
// }
// }
// catch(e) {
// Mojo.Log.error(e);
// }
// searchResultList.mojo.noticeUpdatedItems(0, items);
//}


SearchAssistant.prototype.viewListing = function(event) {
Mojo.Controller.stageController.pushScene("listingView", event.item);
}

16 changes: 16 additions & 0 deletions app/views/listingView/listingView-scene.html
@@ -0,0 +1,16 @@
<div id="listingViewScene">
<div id="header">
<div id="logo"><img src="images/etsy_logo.png"></div>
<div id="navhome" class="capitalize">Listing</div>
</div>
<div class="palm-page-header multi-line">
<div class="palm-page-header-wrapper">
<div id="listingName" class="title left">
</div>
</div>
<div>
<div class="palm-page-text-wrapper">
<div id="listingImage"></div>
<div id="listingDesc" class="palm-body-text"></div>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/search/search-scene.html
Expand Up @@ -3,7 +3,7 @@
<div id="logo"><img src="images/etsy_logo.png"></div>
<div id="navhome" class="capitalize">Search Results</div>
</div>
<div id="searchDescText" class="palm-body-text">Loading...</div>
<div id="searchDescText" class="palm-body-text">Searching, please wait...</div>

<div x-mojo-element="List" id="searchResultList"></div>
</div>
4 changes: 4 additions & 0 deletions sources.json
Expand Up @@ -7,5 +7,9 @@
{
"scenes": "search",
"source": "app/assistants/search-assistant.js"
},
{
"scenes": "listingView",
"source": "app/assistants/listingView-assistant.js"
}
]

0 comments on commit 569b620

Please sign in to comment.