Knockout.jsã®ko.utilsãå°å³ã«ä¾¿å©
å°ç²ãªã¢ããªã±ã¼ã·ã§ã³ã°ãããæ¸ãã¦ããã®ã§ãã¯ã©ã¤ã¢ã³ããµã¤ãMVCãå®è£ ãããæãå°ç²ãªã©ã¤ãã©ãªã§ããã¨ããã®Knockout.jsã使ã£ã¦ãã¾ãã
Knockout.jsã¯MVVMã®ãã¿ã¼ã³ãæä¾ããã©ã¤ãã©ãªãªã®ã§ãããä»åã¯è©³ç´°ãæ¸ãã¾ããã
ç¥ãããæ¹ã¯å
¬å¼ã®ãã¥ã¼ããªã¢ã«ã¨ããã¨ã¦ã親åã§ããæããªã®ã§ããããè¦ããããã®ã§ã¯ãªãã§ããããã
ä»åã¯ko.utilsã®è©±
ã§ãKnockout.jsã®ããã¥ã¡ã³ãä¸ã§ã¯ç®ç«ããªãã®ã§ãããKnockout.jsã«æ¨æºã§æ·»ä»ãã¦ããko.utilsãå°å³ã«ä¾¿å©ã§ãã
ã¾ãjQueryãããã°ã©ãã£ã¦ãã¨ã®ãªãæ©è½ãããã¾ããjQueryã¨åããã¦ä½¿ã£ã¦ããªãå ´åã¨ãã«ã¯é常ã«å©ããã®ã§åå¿é²çã«æ®ãã¾ãã
ã¡ãªã¿ã«å
¬å¼ã®ããã¥ã¡ã³ãããããªã³ã¯ããã¦ããKnock Me Outã¨ããããã°ãåèã«ãã¾ããã
ã©ã¤ãã©ãªã®ä½è
ã®æ¹ãªãã§ãããï¼
å ¨é¨æ¸ãã¨è¨å¤§ãªéã«ãªãã®ã§ãã¨ããããä»åå©ç¨ãã ko.utils.array* ç³»ã«çµãã¾ãã
ko.utils.arrayFilter
é
åããã£ã«ã¿ã¼ãã¦ãã£ã«ã¿ã¼ã«è©²å½ããè¦ç´ ã®ã¿ã®é
åãè¿ãã¾ãã
æ¤ç´¢ã¨ãã«ä½¿ãããã§ãã
class sampleViewModel @sampleArray = ko.observableArray(['foo', 'bar', 'hoge', 'fuga']) @filter = (filter) -> filteredArray = ko.utils.arrayFilter @sampleArray(), (str) -> filter.test str
第ï¼å¼æ°ã«é åã渡ãã¨ç¬¬ï¼å¼æ°ã®é¢æ°ã«ä¸åãã¤é åã®ä¸èº«ã渡ãã¦ããã¾ãã é¢æ°ã®ä¸ã§trueãè¿ããè¦ç´ ã®ã¿ã®é åãæ»ãå¤ã«ãªãã¾ãã
ko.utils.arrayFirst
arrayFilterã¨ã»ã¨ãã©å¤ãããªãã§ãã
ãã£ã«ã¿ã¼ã«è©²å½ããæåã®è¦ç´ ãè¿ãã¾ãã
class sampleViewModel @sampleArray = ko.observableArray(['foo', 'bar', 'hoge', 'fuga']) @filter = (filter) -> filteredArray = ko.utils.arrayFirst @sampleArray(), (str) -> filter.test str , sampleViewModel
ã³ã¼ããã»ã¨ãã©å¤ãããªãã§ããã
ãã¡ãã¯ä½æ
ã第3å¼æ°ã«ãã£ã«ã¿ã¼é¢æ°ãå®è¡ãããªãã¸ã§ã¯ããæå®ã§ãã¾ãã
ko.utils.arrayForEach
æ®éã®forEachã§ãã
class sampleViewModel @sampleArray = ko.observableArray(['foo', 'bar', 'hoge', 'fuga']) @alertAll = -> ko.utils.arrayForEach @sampleArray(), (str) -> alert str
ç¹ã«ãããã¨ãªãã§ããâ¦ã
ko.utils.arrayGetDistinctValues
é åã®ä¸ã§ã¦ãã¼ã¯ãªå¤ã ããåãåºããé åãè¿ãã¾ãã
class sampleViewModel @sampleArray = ko.observableArray(['foo', 'bar', 'hoge', 'foo']) @unique = -> ko.utils.arrayGetDistinctValues @sampleArray() # -> ['foo', 'bar', 'hoge']
ããã°ã·ã¹ãã ã§ã«ãã´ãªã¿ã°ã¨ãã¤ããã¨ãã«ä½¿ããã§ããããã
ko.utils.arrayIndexOf
é åã®ä¸ã§ããè¦ç´ ã®ä½ç½®ãæ¢ãã¦è¿ãã¾ãã
class sampleViewModel @sampleArray = ko.observableArray(['foo', 'bar', 'hoge', 'foo']) @index = (obj) -> ko.utils.arrayIndexOf @sampleArray(), obj
ã¡ãªã¿ã«åãè¦ç´ ãé
åã«å
¥ã£ã¦ããå ´åãå
ã«è¦ã¤ãã£ãæ¹ãè¿ãã¾ãã
該å½ããè¦ç´ ããªãå ´åã¯-1ãè¿ãã¾ãã
ko.utils.arrayMap
æ®éã®Mapã§ãã
class sampleViewModel @sampleArray = ko.observableArray(['foo', 'bar', 'hoge', 'foo']) @mappedArray = -> ko.utils.arrayIndexOf @sampleArray(), (str) -> 'mapped' + str
第1å¼æ°ã§ä¸ããããé åã®åè¦ç´ ã«ç¬¬2å¼æ°ã®é¢æ°ãå®è¡ããé¢æ°ã®æ»ãå¤ã§æ°ããé åãä½ãã¾ãã
ko.utils.arrayPushAll
é åã«é åãç¶ã足ãã¾ãã
class sampleViewModel @sampleArray = ko.observableArray(['foo', 'bar', 'hoge', 'foo']) @pushedArray = (array) -> ko.utils.arrayPushAll @sampleArray(), array # -> ['foo', 'bar', 'hoge', 'foo', array[0], array[1], ...]
ko.utils.arrayRemoveItem
é åããæå®ããè¦ç´ ãåé¤ãã¾ãã
class sampleViewModel @sampleArray = ko.observableArray(['foo', 'bar', 'hoge', 'foo']) @removedArray = -> ko.utils.arrayIndexOf @sampleArray(), 'foo' # -> undefined @sampleArray() # -> ['bar', 'hoge', 'foo']
ã¾ã¨ã
observableArrayã¨ãã§é åãå¤ãæ±ããã¨ãããã®ã§ãko.utils.array*ç³»ã¯è¦ãã¦ããã¨ä¾¿å©ã