Week 4: Building a full gallery app

Introduction

Over the last couple of weeks, we've learned how to separate that from what you're showing from how you show it.
So far we've looked at how to represent one image using a single object. But we wanna build massive websites with hundreds, if not thousands of images, and so we need to look at different techniques that don't build aside one object at a time, but
can represent masses and masses of data.
GOAL TO REACH:

  1. Store objects in an array and displays them with a template
  2. Dynamically display single images from an array
  3. Implement a search function
  4. Work with complex data structures
  5. Switching between different templates

JavaScript arrays

We need a new data structure, to represent lots of data using one template -> Array, which can store lots of objects.
Create: var images = ["a","b","c","d"];
Find: images[1];
Add data: images[1] = 3;

Storing objects in arrays and displaying them with a template

In JS, there are two most important ways to representing data. (1) object (2) array
The template for array, it is designed for lots of images.


The template for array:

Dynamically displaying single images from an array

We want to get a bigger vision of each foto when we click it. -> but here comes the question: how did we know, which img will be grap when we click? How can we display a single img which is right from an array?

to get the right image

we using jQuery to get hold of all the divs with class thumbnail.
// $(this) is jQuery. It refers to the thing(the div) they clicked on.
// data-id There is a special type: the-attribute-of-data. it's nothing else but start with data-. -> but it doesn't do anything to HTML style or others, it just used to add extra data onto out HTML elements, so we can use it later in our JavaScript. -> data-id="{{@index}}"hold the identifier(for example n), and the identifier is pointing to the images[n] -> .data("id"); is jQuery code, we use jQuery.data to get the value of the attribute id.
// {{@index}}: there is actually no variable in our array called index. But @index is going to give you the number of the current element in the array as long as the template is going through an array. -> like array[5], then data("id") will be 5.

class thumbnail

Implementing a search function

3 Step to do:
(1) recognize when sb. is searching a word and figure out what is the word
(2) search through all images in array and find all matches
(3) put the result in new array and display them

step (1) - Recognize
// .keypress() jQuery function .keypress() will recognize when sb. types and texts in the box
// if(e.which==13){ ... } the variable e is speicial, he passed into functions every time we call event. Here e tell us the key on the keypress-event.
every letter has a number, 13 here stands also a letter!
(You can check here for all KEYCODE, see the Dec and Description columns!)

step 1

step(2) Searching
// images: Creating a new array
// data.images.filter(funtion(d)) The function .filter() from jQuery takes an array and looks through that array, finding only those elements that obey a certain property. -> it can be understood like this: it should be in this array only it is a element of data.images && it match to the anonymous function(d)

step 2

//

Data structure for a complete image gallery

Writing the templates for the gallery

Switching displays in the gallery

Summary

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末预鬓,一起剝皮案震驚了整個濱河市掷倔,隨后出現(xiàn)的幾起案子燃辖,更是在濱河造成了極大的恐慌污它,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件俱两,死亡現(xiàn)場離奇詭異舞骆,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)糊饱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門垂寥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事滞项∠凉椋” “怎么了?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵文判,是天一觀的道長过椎。 經(jīng)常有香客問我,道長戏仓,這世上最難降的妖魔是什么疚宇? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮赏殃,結(jié)果婚禮上敷待,老公的妹妹穿的比我還像新娘。我一直安慰自己仁热,他們只是感情好榜揖,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著抗蠢,像睡著了一般举哟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上物蝙,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天炎滞,我揣著相機(jī)與錄音,去河邊找鬼诬乞。 笑死,一個胖子當(dāng)著我的面吹牛钠导,可吹牛的內(nèi)容都是我干的震嫉。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼牡属,長吁一口氣:“原來是場噩夢啊……” “哼票堵!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起逮栅,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤悴势,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后措伐,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體特纤,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年侥加,在試婚紗的時候發(fā)現(xiàn)自己被綠了捧存。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖昔穴,靈堂內(nèi)的尸體忽然破棺而出镰官,到底是詐尸還是另有隱情,我是刑警寧澤吗货,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布泳唠,位于F島的核電站,受9級特大地震影響宙搬,放射性物質(zhì)發(fā)生泄漏警检。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一害淤、第九天 我趴在偏房一處隱蔽的房頂上張望扇雕。 院中可真熱鬧,春花似錦窥摄、人聲如沸镶奉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽哨苛。三九已至,卻和暖如春币砂,著一層夾襖步出監(jiān)牢的瞬間建峭,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工决摧, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留亿蒸,地道東北人。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓掌桩,卻偏偏與公主長得像边锁,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子波岛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容