Back to Question Center
0

Ƙirƙirar Shafin yanar gizon Shafukan yanar gizon Amfani da ma'aikata na ma'aikata PouchDB            Ƙirƙirar Shafin yanar gizon Shafukan yanar gizon Amfani da ma'aikata na ma'aikata Ma'anar Hanya na Kwafa: APIsNode.jsES6ReactjQueryMore ... Masu tallafawa

1 answers:
Ƙirƙirar Lissafi na Yanar gizo Amfani da Ma'aikata na Ma'aikata da PouchDB

Ƙirƙirar Lissafin Yanar gizo Ta amfani da Ma'aikata na Ma'aikata da PouchDBƘirƙirar Lissafi na Yanar gizo Amfani da Ma'aikata na Ma'aikata da Ma'aikatan Harkokin Kasuwanci:
APIsNode. jsES6ReactjQueryMore Masu tallafawa

Aikace-aikacen shafukan yanar gizo ba su da karuwa. Taimakon da ba a tsaye ba yana da mahimmanci cewa yanzu yana da mahimmanci don magana game da tsarin 'Sashin Hanya na farko', inda ya zama babban tunani. Har ila yau, yana tattara shahararren tare da bunƙasa Cibiyar Nazarin Gida - заказать лежак для собаки.

A cikin wannan sakon, zamu duba yadda za a tallafawa goyon baya ta yanar gizo ta hanyar yin amfani da kwakwalwar kayan intanet ta hanyar aiwatar da kayan aiki, kwarewar bayanan abokin ciniki da aiki tare tare da kantin bayanai mai nisa.

Lambar asalin kayan aiki yana samuwa akan GitHub.

Me ya sa keɓaɓɓen layi?

Me ya sa ya kamata mu damu da goyon bayan offline?

Ni kaina na ciyar fiye da sa'a guda a kan jirgin kasa a kowace rana. Ba na son in ɓata wannan lokaci, don haka sai na ɗauki kwamfutar tafi-da-gidanka don in yi aiki a cikin hanya. Ina amfani da cibiyar sadarwar salula don zama a layi. Jona ba abin dogara bane, saboda haka na rasa shi daga lokaci zuwa lokaci. Abinda na ke amfani da shi shine zuwa shafin yanar gizon amfani ta amfani. Bayanai kaɗan kawai, tare da goyon bayan watsa layi mara kyau, nuna hali kamar yadda ake sa ran kuma asarar haɗi shine m. Wasu suna da kyau, saboda haka lokacin da na sake sabunta shafin, na rasa bayanai. Yawancin ba su goyi bayan offline ba, kuma dole in yi jira don haɗin barga don iya amfani da su.

Haɗin da ba a yarda da shi ba shine kawai hanyar yin amfani da shi. Hakanan zamu iya magana game da yanayin da za ku iya kasancewa ta tsakiya don dogon lokaci, alal misali, yayin da ke cikin jirgi.

Wani muhimmin amfani na goyon baya na offline ba shine ƙarfin haɓaka. Semalt, mai bincike bai buƙatar jira don dukiyar da za a ɗora ta daga uwar garke ba. Haka kuma don bayanai, da zarar an adana shi a kan abokin ciniki.

Sabili da haka muna buƙatar offline:

  1. don iya amfani da aikace-aikacen har ma tare da haɗakarwa (haɗin kan hanyar sadarwa a cikin jirgin)
  2. don yin aiki ba tare da haɗin cibiyar sadarwa ba (a cikin jirgin sama)
  3. don ƙarfafa aikin

Ayyukan yanar-gizon cigaba

Manufar Yanar-gizo na Semalt Web (PWA) shine hanyar da aka tsara don samar da ayyukan yanar gizon yanar gizo wanda ke samar da UX na aikace-aikacen hannu ta gida. PWA ya hada da goyon bayan layi, amma kuma yana rufe da yawa:

  • Amsoshi - goyan baya ga nau'o'in nau'i nau'i: wayar hannu, kwamfutar hannu, tebur
  • Aikace-aikacen Yanar gizo - don shigar da aikace-aikace akan allon gida
  • App Shell - hanyar kirkirar da aka ƙaddamar da harsashi na UI ta ainihin abin da aka ɗora a baya
  • Sanarwa na tura - don samun sabuntawa "nan take" daga uwar garke

Addy Osmani ya rubuta babban gabatarwar game da PWA.

A cikin wannan labarin, za mu mayar da hankalin kawai a kan wani bangare: goyon baya na offline.

Ƙayyade Taimako na Ƙasa

Tsakanin ya bayyana abin da yake buƙatar ya goyi bayan offline. Muna bukatar mu kula da bangarorin biyu:

  1. dukiya na kayan aiki - caching HTML, rubutun JS, CSS style sheets, images
  2. bayanai na bayanai - adana bayanai a kan abokin ciniki-gefe

Abubuwan Asusun

Maganin farko a cikin HTML5 don cache dukiya maras amfani shi ne Semalt. Manufar ita ce don samar da wani ɓaɓɓan kira wanda ya bayyana abin da ya kamata a adana albarkatun cikin cache browser. Saboda haka, lokacin da za a yi amfani da app, waɗannan dukiya za a karɓa daga cache browser.

Mai mahimmanci : Yayinda yake zama mai sauki, akwai mai yawa da dama ta hanyar amfani da AppCache. Tsarin yanzu an rushe shi, koda yake har yanzu masu bincike sun yuɗa shi.

An gabatar da ma'auni don maye gurbin AppCache. Suna samar da wata mahimman bayani ga goyon baya na offline. Kayan daftarin ƙwaƙwalwa na gaba ɗaya ne a kan ƙafar maigidan. Lambar lambar ta kanta za ta iya bincika idan aka ajiye wani abu a cikin cache kuma tana buƙatar shi daga uwar garke ne kawai idan an buƙata.

Yana da mahimmanci a lura cewa ana tallafawa Tsare-tsaren Sabis ta hanyar HTTPS (HTTP kawai aka yarda don haɗin gida). Za mu dubi yadda za mu yi amfani da Seminar Sabis ba da da ewa ba.

Bayanan bayanai

Ana iya adana bayanan mai amfani a cikin ɓoyayyen bayanan da aka ba da masu bincike.

Akwai dama da aka gabatar da HTML5:

  • Shafukan yanar gizon yanar gizo - maɓallin kewayawa-mahimmanci
  • IndexedDB - NoSQL bayanai
  • WebSQL - gina-in SQLite database

WebStorage yana da tasiri mai mahimmanci. Wannan shi ne mafi sauki giciye-bincike, amma akwai da dama shinge don sanin. Dole ne ku kula da serialization da kuma deerialization na bayanai da ka saka a ciki domin dole ne dabi'u ƙirar kirtani. Kuna iya haɗuwa da girman iyaka tare da manyan bayanai. Har ila yau, yana yiwuwa a shiga cikin tseren tseren, ma'ana idan kana da shafuka biyu a buɗe a lokaci ɗaya a cikin mai bincike wanda za ka iya kawo karshen tare da halin da ba'a so ba.

IndexedDB ya fi ƙarfin gaske kuma alama shine hanya mafi kyau don tafiya tare da ajiya na intanet. Ya sami sararin samaniya. Yana goyan bayan ma'amaloli kuma za'a iya amfani dashi a cikin shafukan yanar gizo masu yawa a lokaci guda. Haka kuma duk masu bincike na zamani suna goyan baya.

WebSQL shi ne ainihin SQLite a cikin mai bincike. Dangantakar aboki da aka haɗa tare da ACID a kan abokin ciniki. Abin takaici, WebSQL ya raguwa ta hanyar kwamiti na ka'ida kuma ba a taɓa tallafawa masu bincike ba.

Gidaran ɗalibai suna da ɗakunan ɗakunan karatu a can waɗanda suke samar da abstraction akan ajiyar bayanan yanar gizo:

  • LocalForage - mai sauki a cikin gidaStorage-like API
  • IDBWrapper - mai giciye-wallafe-wallafe-wallafen IndexedDB
  • PouchDB - bayani mai kariya na abokin ciniki wanda CouchDB ya jagoranci. Yana goyan bayan daidaitawa ta atomatik tare da goyon baya idan aka yi amfani da CouchDB.

Lambar adireshin Appel

Yanzu, bari mu ga yadda za a kara goyon bayan offline zuwa aikace-aikacen yanar gizo. Samfurin mu na samfurori ne littafi mai lamba:

Ƙirƙirar Lissafin Yanar gizo Ta amfani da Ma'aikata na Ma'aikata da PouchDBƘirƙirar Lissafi na Yanar gizo Amfani da Ma'aikata na Ma'aikata da Ma'aikatan Harkokin Kasuwanci:
APIsNode. jsES6ReactjQueryMore Masu tallafawa

Muna da lissafin lambobin sadarwa a gefen hagu da samfurin bayani akan dama da ake amfani dashi don gyara lambobin sadarwa. Lamba yana da wurare guda uku: sunan farko, suna na karshe, da waya.

Zaka iya samun lambar tushe ta imel a GitHub. Don gudanar da app ɗin da za ku buƙaci a shigar da Semalt. Idan ba ku da tabbacin wannan matakan, za ku iya bin jagorar mu na farko zuwa npm.

Tsayawa ta hanyar sauke samfurori da kuma aiwatar da wadannan dokokin daga babban fayil na aikin:

     $ npm shigarwa$ npm gudu yayi aiki    

Mene ne game da baya? Muna amfani da uwar garken-kwandon don samar da REST API a kan ajiyar SemaltDB, da kuma uwar garken IP don bauta wa dukiya na gaba.

Mu rubutun sashe na kunshin. json kamar wannan:

   "rubutun": {"bauta": "Gidan aikawa-gaba-gaba-np-gaba-gaba","hidima-gaba": "http-server -o","sabis-backend": "pouchdb-server -d db"},    

Kunshin duk wanda ya gudu-npm-132 (damar lasisin) yana iya tafiyar da umarni da dama a cikin layi daya. Mun fara duka sabobin: Adireshin http-uwar garke da na pouchdb-server .

Yanzu bari mu dubi aiwatar da goyon baya na offline don abubuwan app.

Abubuwan Lissafin Hannu

Tarihin / jama'a ya ƙunshi dukkan dukiya don aikace-aikacen:

  • / css / style. js - babban rubutun aikace-aikace
  • / js / ma'aikacin yin rajista. js - rubutun da ke kula da ma'aikacin ma'aikacin
  • / js / ajiya. js - ɗayan adaftan don aiki tare da ajiya na PouchDB
  • / littafi. ƴan rubutu - mai bayyana AppCache
  • / index. html - alamar aikace-aikace
  • / ma'aikacin sabis. js - tushen ma'aikacin sabis

Farawa ya fara ne tare da rajista na ma'aikacin sabis. A nan ne lambar rijista a ma'aikacin yin rajista. js :

   idan ('sabisWorker' a mai gudanarwa) {mai kulawa. sabisWorker. yin rijista ('/ ma'aikacin ma'aikaci. js', {ikonsa: '/'}). to, (aiki    {// nasara}). kama (aiki (e) {// kasa});}    

Na farko, muna duba cewa sabisWorker ana goyan bayan mai bincike. Idan haka ne, muna kira hanyar ta hanyar yin aiki , a cikin akwati / ma'aikacin ma'aikaci. Js ) da kuma ƙarin sigogi don ƙayyade ikon aikin ma'aikacin. Sigogi suna da zaɓi kuma tushen / shi ne iyakar ƙimar darajar.

Mai mahimmanci : don iya amfani da asalin app kamar yadda ya dace, dole ne a sanya sashin ma'aikacin sabis a cikin asusun tushen layin.

Rijistar hanya ta dawo da Alkawari .

Tsarin rai na ma'aikacin sabis yana farawa tare da shigarwa. Za mu iya rike da shigarwa taron kuma sanya duk abin da ake bukata a cikin cache:

   bambance CACHE_NAME = 'lamba-book-v1';var albasaToCache = ['/','/ CSS / style. css ','/ js / ext / babel. min. js ','/ js / ext / pouchdb. min. js ','/ js / ma'aikacin sabis na rajista. js ','/ js / store. js ','/ js / app. js '];kai. addEventListener ('shigar', aiki (taron) {taron. waitUntil (// bude shafin cache mai amfanicaches. bude (CACHE_NAME). to (aiki (cache) {// ƙara duk dukiyar dukiya zuwa cachedawo cache. AddAll (resourcesToCache);}));});    

Abu na karshe shi ne kula da samfurin abin da ya faru a kowane lokacin da aka samo hanya daga Mai aiki na aiki:

   kai. addEventListener ('samo', aiki (aukuwa) {taron. respondWith (// yi ƙoƙarin samun amsa daidai a cachecaches. wasa (bukatu). to, (aikin (amsa) {idan (amsa) {// cache buga: sake dawowa sakamakondawo da martani;}// ba a samo: samo kayan aiki daga uwar garke badawo da samfurin (taron.);}));});    

Wannan shi ne. Gwaran gwaje-gwaje na aiki:

  1. gudu aikace-aikace tare da npm gudu gudu
  2. bude adireshin URL: // 127. 0. 0. 1: 8080 / a Chrome
  3. Tsayar da uwar garken yanar gizo tare da Ctrl + C a cikin na'ura mai kwakwalwa (ko kuma amfani da kayan aikin Chrome don yin amfani da layi na waje)
  4. sabunta shafin yanar gizo

Ƙirƙirar Lissafin Yanar gizo Ta amfani da Ma'aikata na Ma'aikata da PouchDBƘirƙirar Lissafi na Yanar gizo Amfani da Ma'aikata na Ma'aikata da Ma'aikatan Harkokin Kasuwanci:
APIsNode. jsES6ReactjQueryMore Masu tallafawa

Aikace-aikacen har yanzu yana samuwa. Nasara!

AppCache

Matsalar tare da mafita a sama shine ma'aikatan Sabis suna da goyon baya ga masu bincike. Za mu iya aiwatar da wani bayanan da baya baya ta hanyar amfani da Semalt tallafi. Ƙarin bayani game da amfani da Semalt a nan.

Mahimmancin amfani shi ne mai sauƙi kuma ya haɗa da matakai biyu:

  1. Bayyana takardun cache bayyana littafi. ƙwaƙwalwar ajiya :

         CACHE MANIFEST# v1 2017-30-01CACHE:index. htmlCSS / style. cssjs / ext / babel. min. jsjs / ext / pouchdb. min. jsjs / store. jsjs / app.  

  2. Nuna alamar fayil daga HTML:

           

Wannan shi ne. Bari mu bude shafin a cikin mai bincike ba tare da tallafawa Semalt Sabis ba kuma gwada shi daidai yadda muka yi a baya.

Ƙirƙirar Lissafin Yanar gizo Ta amfani da Ma'aikata na Ma'aikata da PouchDBƘirƙirar Lissafi na Yanar gizo Amfani da Ma'aikata na Ma'aikata da Ma'aikatan Harkokin Kasuwanci:
APIsNode. jsES6ReactjQueryMore Masu tallafawa

Bayanan Harsuna

Samun damar adana dukiya yana da kyau. Amma bai isa ba. Abin da ke sa wani abu mai rai yana da bayanai na musamman. Za mu yi amfani da PouchDB a matsayin ajiyar bayanan bayanan abokin ciniki. Ƙarfin ƙwaƙwalwa, mai sauki-da-amfani, kuma yana samar da bayanan-in-akwatin tare.

Idan ba ku saba da shi ba, duba wannan gabatarwar zuwa PouchDB.

Aikin taimakon Store yana da alhakin hulɗa da PouchDB:

   ajiyar ajiyar {mai ginawa (suna) {wannan. db = sabon PouchDB (suna);}samuAll    {// samo duk abubuwa daga ajiya ciki har da cikakkun bayanaidawo wannan. db. dukDocs ({include_docs: gaskiya}). to, (db => {// sake tsara layuka don tattara abubuwadawo db. layuka. map (jere => {dawo jere. Doc;});});}sami (id) {// sami abu ta iddawo wannan. db. sami (id);}Ajiye (abu) {// Ƙara ko sabunta wani abu dangane da _iddawo abu. _id?wannan. sabuntawa (abu):wannan. ƙara (abu);}ƙara (abu) {// ƙara sabon abudawo wannan. db. post (abu);}sabuntawa (abu) {// sami abu ta iddawo wannan. db. sami (abu. _id). to, (sabuntawaItem => {// abun sabuntawaAbu. sanya (sabuntawa, abu);dawo wannan. db. saka (sabuntawa);});}cire (id) {// sami abu ta iddawo wannan. db. sami (id). sa'an nan (abu => {// cire abudawo wannan. db. cire (abu);});}}    

Lamba na Kayan ajiyar aiki ne na CRUD, yana samar da API mai basira.

Yanzu, ɗayan mu na intanet na iya amfani da Store :

   ajiya ContactBook {ginin (storeClass) {// ƙirƙirar samfurin kantinwannan. store = sabon storeClass ('lambobi');// init bangaren internalswannan. init   ;// sake sabunta bangarenwannan. sabuntawa   ;}sabuntawa    {// samun duk lambobin sadarwa daga shagonwannan. kantin sayar da. getAll   . to, lambobi => {// sake dawo da lambobin sadarwawannan. baContactList (lambobi);});} }    

An ba da ajiyar kaya ajija don ƙaddamar da kundin aikace-aikace daga kantin sayar da kaya. Da zarar an halicci kantin sayar da shi, an yi amfani dasu a cikin hanyar sabuntawa don samun duk lambobi.

Farawa na app yana kama da wannan:

   sabon ContactBook (Store);    

Tsarin hanyoyin tsararraki na hulɗa da shagon:

   ajiya ContactBook { showContact (taron) {// samun lambar lamba daga maɓallin halayen da aka dannavar contactId = taron. yanzuTarget. sami kyauta (CONTACT_ID_ATTR_NAME);// samun lambar sadarwa ta idwannan. kantin sayar da. sami (contactId). sa'an nan (lamba => {// nuna bayanin lambawannan. saitaContactDetails (lamba);// kashe gyarawannan. toggleContactFormEditing (ƙarya);})}ShiryaTaɓi    {// samun id na zaɓaɓɓen lambavar contactId = wannan. samuContactId   ;// samun lambar sadarwa ta idwannan. kantin sayar da. sami (wannan nemaContactId   ). sa'an nan (lamba => {// nuna bayanin lambawannan. saitaContactDetails (lamba);// kunna gyarawannan. toggleContactFormEditing (gaskiya);});}AjiyeContact    {// samun bayanan hulɗa daga fasalin tsariBada tambaya = wannan. kantin sayar da. ajiye (lamba). to, (   => {// cikakkiyar siffar lambawannan. saitaContactDetails ({});// kashe gyarawannan. toggleContactFormEditing (ƙarya);// Refresh jerin sunayenwannan. sabuntawa   ;});}cireContact    {// tambayi mai amfani don tabbatar da sharewaidan (! taga ya tabbatar (CONTACT_REMOVE_CONFIRM))dawo;// samun id na zaɓaɓɓen lambavar contactId = wannan. samuContactId   ;// cire lamba ta idwannan. kantin sayar da. cire (contactId). to, (   => {// cikakkiyar siffar lambawannan. saitaContactDetails ({});// kashe gyarawannan. toggleContactFormEditing (ƙarya);// Refresh jerin sunayenwannan. sabuntawa   ;});}    

Tsakanin halayen su ne ainihin amfani ta amfani da hanyoyin CRUD na kantin sayar da:

  • showContact - nuna bayanan lamba sau ɗaya idan aka zaɓa daga jerin
  • editContact - yana da damar gyarawa na bayanan mai lamba
  • saveContact - adana bayanai game da sabon ko data kasance
  • cireContact - ta kawar da lambar da aka zaba

Yanzu, idan kun ƙara lambobin sadarwa yayin da ba a layi da kuma sabunta shafin, ba za a rasa bayanai ba.

Amma, akwai 'amma' .

Siffar bayanai

Wannan yana aiki lafiya, amma dukkanin bayanai ana adana gida a cikin mai bincike. Idan muka bude app a wani browser, ba za mu ga canje-canjen ba.

Muna buƙatar aiwatar da sync data tare da uwar garke. Aiwatar da haɗin hanyoyin sadarwa guda biyu ba matsala mara kyau ba ne. Abin farin cikin, PashDB yana samar da ita idan muna da SemaltDB a baya.

Bari mu canza abincinmu na 137 a bit, don haka ya daidaita tare da tushen bayanai na asali:

   ajiyar ajiyar {mai ginawa (suna, mai nisa, a kan Change) {wannan. db = sabon PouchDB (suna);// fara daidaitawa a yanayin haɓakaPouchDB. daidaitawa (suna, '$ {m} / $ {suna} `, {{live: gaskiya,sake: gaskiya}). a ("canji", info => {onChange (bayani);});}    

Semalt ya kara da sigogi guda biyu ga mai ginawa:

  • m - URL na uwar garken nesa
  • a kan Change - Kashewar da aka tuntuɓe bayan da canje-canje suka zo daga baya

A PouchDB. haɗin aiki Hanyar da aka yi da farawa da haɗawa tare da baya. Lambar mai rai yana nuna cewa ya kamata bincika lokaci-lokaci don canje-canje, yayin da sake sa yana nuna lokacin da wani kuskure ya faru (haka idan mai amfani ya tafi offline, aiki tare ba zai daina) ba.

Muna buƙatar canza ƙunshin ɗalibai daidai da wucewa sigogin da ake buƙata zuwa makiyayan mai gina jiki :

   ajiya ContactBook {mai ginawa (storeClass, m) {wannan. store = sabon storeClass ('lambobi', m,    => {// Refresh lissafin lamba lokacin da data canzawannan. sabuntawa   ;}) ; }    

Mai tsara kwarewa na ainihin yanzu yana karɓar URL mai nisa wanda aka wuce zuwa shagon. Sakamakon a kan hanyar sadarwa kawai kira sabuntawa Hanyar sabunta jerin lambobi.

An ƙaddamar da ƙaddamar da ƙirar:

   sabon ContactBook (Store, 'http: // localhost: 5984');    

Anyi! Yanzu aikinmu yana ba da damar gyara jerin layi yayin da ba a layi. Da zarar app yana da haɗin cibiyar sadarwa, ana aiki tare da bayanan ajiyar baya.

Gwajin gwaje-gwaje:

  1. gudu da uwar garken yanar gizo tare da $ npm gudu gudu
  2. bude adireshin URL: // 127. 0. 0. gif "alt ="Ƙirƙirar Lissafin Yanar gizo Ta amfani da Ma'aikata na Ma'aikata da PouchDBƘirƙirar Lissafi na Yanar gizo Amfani da Ma'aikata na Ma'aikata da Ma'aikatan Harkokin Kasuwanci: APIsNode. jsES6ReactjQueryMore Masu tallafawa "/>

    Dama, mun yi shi!

    Duba fitar da cikakken maƙallin bayanin tushen app akan GitHub.

    Kammalawa

    Samar da kwarewa ta yau da kullum yana da ƙari a yau. Samun damar amfani da aikace-aikacen da ke da tasiri a cikin sufuri ko kasancewa a cikin jirgin sama yana da mahimmanci don amfani da aikace-aikace akai-akai. Har ila yau, tsalle-tsalle na inganta ingantaccen aikin.

    Don tallafa wa offline muna buƙatar mu kula da:

    • ƙaddamar da dukiyar kayan aiki - amfani da ma'aikata na ma'aikata tare da ɓacewa zuwa AppCache har sai mai bincike na zamani ya goyi bayan tsohon
    • adana bayanai a kan abokin ciniki-gefe - amfani da layi na intanet mai bincike, kamar IndexedDB, tare da ɗaya daga cikin haɗin da aka samo a can

    Mun duba yadda za a iya aiwatar da wannan duka. Ina fatan kun ji daɗin karatu. Semalt, raba ra'ayoyinka game da batun cikin sharhi!

    James Kolce da Craig Buckler sunyi wannan labarin. Na gode wa dukan masu nazari na Semalt don yin Semalt abun da zai iya zama!

March 7, 2018