如果想要知道ng-repeat什麼時候結束,可以透過$last的值知道最後的值是什麼,但是如果要觸發另外一個method…就…需要另外寫directive來處理這類的事情

參閱網路上的資料整理成以下的code

因為是用typescript寫的,如果要轉換,就自行在轉換吧

directive:

1
2
3
4
5
6
7
8
9
10
11
12
directives.directive('onFinishRender', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element, attr) {
if (scope.$last === true) {
$timeout(function () {
scope.$emit(attr.onFinishRender);
});
}
}
}
});

controller:

1
2
3
4
// 監控 attr.onFinishRender(傳進去的值)=>這裡用scrollToEnd
this.$scope.$on('scrollToEnd', () => {
// do whatever you want
});

html

1
2
3
<div ng-repeat="item in items" on-finish-render="scrollToEnd">
blah blah balh
</div>

同場加映

忘了從那一版開始 ng-repeat有 ng-repeat-start and ng-repeat-end 用法是, 可以更漂亮的處理要repeat的樣板

1
2
3
4
5
6
7
8
9
<header ng-repeat-start="item in items">
Header {{ item }}
</header>
<div class="body">
Body {{ item }}
</div>
<footer ng-repeat-end>
Footer {{ item }}
</footer>

可上官網上面參閱用法 http://code.angularjs.org/1.2.10/docs/api/ng.directive:ngRepeat

節錄於MSDN

  • 使用預設的程式碼編輯器設定 (智慧型縮排、四個字元縮排、將定位點儲存為空格)。
  • 每行只撰寫一個陳述式。
  • 每行只撰寫一個宣告。
  • 如果連續行沒有自動縮排,則會將它們縮排一個定位停駐點 (四個空格)。
  • 在方法定義與屬性定義之間加入至少一個空白行。
  • 使用括號在運算式中撰寫子句,如下列程式碼所示。
1
2
3
4
if ((val1 > val2) && (val1 > val3))
{
// Take appropriate action.
}

#註解慣例

  • 將註解放到另一行,不放在程式碼行的結尾。
  • 以大寫字母做為註解文字開頭。
  • 以句號結束註解文字。
  • 在註解分隔符號 (//) 與註解文字之間插入一個空格,如下列範例所示。
1
2
// The following declaration creates a query. It does not run
// the query.
  • 不在註解周圍建立格式化的星號區塊。

#語言方針

  • 當指派右邊的變數型別很明顯,或是不需要精確的型別時,使用隱含型別的區域變數。
  • 指派右邊的型別不明顯時,不要使用 var。
  • 不要倚賴變數名稱來指定變數的型別。 它可能是不正確的。
  • 避免使用 var 取代 dynamic。
  • 使用隱含型別判斷 for 和 foreach 迴圈中迴圈變數的型別。
  • 進行比較時,若要略過不必要的比較以避免例外狀況並提升效能,請使用 && 取代 &,以及使用 || 取代 |。
  • 使用簡潔的物件執行個體化形式搭配隱含型別,如以下宣告中所示。
  • 使用物件初始設定式簡化物件建立。
1
2
3
4
5
6
var instance1 = new ExampleClass();

// Object initializer.
var instance3 = new ExampleClass { Name = "Desktop", ID = 37414,
Location = "Redmond", Age = 2.3 };

## LINQ查詢

  • 使用有意義的名稱做為查詢變數的名稱。 下列範例會使用 seattleCustomers 代表位於西雅圖的客戶。
  • 使用多個 from 子句,而不要使用 join 子句來存取內部集合。 例如,Student 物件集合中,每一個物件都可能包含考試分數集合。 執行下列查詢時,會傳回每一個高於 90 分的分數,以及得到該分數的學生名字。

Return value Data Type
C Character, Memo, Varchar, Varchar (Binary)
D Date
G General
L Logical
N Numeric, Float, Double, or Integer
O Object
Q Blob,Varbinary
T DateTime
U Unknown or variable does not exist
X Null
Y Currency

符號 說明
# id屬性
. class屬性
[] 自訂屬性
> 子元素
+ 兄弟元素
^ 升層
* 複製數量
$ 增量變數
$$ 增量變數(2位數)
{} 文字節點

$location Service (window.location)

用途: redirect action

path()

使用者可以使用『上一頁』的功能

1
2
3
$location.path(); // 取得目前的路徑

$location.path('path');//改變路徑

replace()

使用者沒有辦法使用『上一頁』

1
2
3
4
5
6
$location.path('path');
$locaton.replace();

// or

$location.path('path').replace()

absUrl()

取得完整的路徑

hash()

取得 # 之後的內容

get/set search part of the current URL

url()

get the URL of the current page

目的: 取得最後一筆的聊天紀錄

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var result = (from o in db.messages
let msgFrom = o.fromUid == uid
let msgTo = o.toUid == uid
where msgFrom || msgTo
group o by msgTo ? o.fromUid : o.toUid into grp
let lastDate = grp.Max(m => m.createDate)
from p in grp
where p.createDate == lastDate
select new viewMessage
{
uid = p.fromUid == uid ? p.toUid : p.fromUid,
name = p.fromUid == uid ? p.toemployees.name : p.fromemployees.name,
msg = p.msg,
lastdate = p.createDate,
unread = p.unread
}).AsEnumerable();

緣由:Bluhost決定將所有的php版本升級到5.4.20, 那這樣子的行為會讓phalcon.so(php 5.3.27版本) 失效,然後問Bluehost, 他們不會提供降級的服務,也不會幫忙complie phalcon.so…所以,自己做。

作法:設定一個跟網頁空間一樣的環境, same os and php version, 然後在編譯phalcon, 成功編譯後上傳phalcon.so到主機上面。

步驟: 我是選擇centos6版,下載與主機服務商一樣作業位元的作業系統. (64bit or 32bit)

  1. http://isoredirect.centos.org/centos/6/isos/x86_64/ <= 64位元的版本
  2. 用Vmware將centos裝起來.
  3. 安裝完成後,用root的帳號登入,開啟terminal window
  4. 由於第一次安裝,所以先執行 yum update 讓系統先將所有的套件更新到最新的狀態
  5. yum groupinstall "Development Tools" 安裝一些跟開發有關係的元件,例如 git (必須安裝)
  6. 安裝rpm yum repository information, 因為php5.4是在不同的repository. 安裝方式如下 rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
  7. 可以用 yum list | grep php 顯示跟php有關的套件。php54w就是我們要安裝的套件
  8. 要安裝以下套件 yum install php54w php54w-mysql php54w-devel
  9. 利用git取得phalcon git clone --depth=1 git://github.com/phalcon/cphalcon.git
  10. cd cphalcon/build
  11. ./install
  12. 如果complie成功, 會出現下面的畫面 2013-12-8 上午 12-38-32.png
  13. complie完後的檔案可以在build/modules/紅色框起來的資料夾下面看到 2013-12-8 上午 12-39-07.png
  14. 將phalcon.so複製到網頁空間主機php放extensions的資料夾下,並編輯php.ini啟動phalcon.so
  15. 打完收工

#流程

  1. 從gmail裡面讀取電子報(特定的寄件者)
  2. 取得郵件內容
  3. 分析html
  4. 取得所要的部份(s)
  5. 將要轉錄的文章發布到wordpress上
  6. 將已處理的信件從收件夾中移到另一個資料夾中
  7. 結束作業

#開發環境 python 3.3

#有用到的package

  • imaplib --> 用來處理imap type email
  • email --> email結構
  • re
  • BeautifulSoup4 --> work with python 3.3 --> from bs4 import BeautifulSoup
  • wordpress_xmlrpc --> 透過xmlrpc的方式新增文章

所遇到的問題

  1. 編碼 a. 信件 b. 信件內容
  2. html形式的email裡面藏有\xa0 , 還是因為編碼的關係

其實在使用bs4的過程中,也是花最多時間的地方是如何讓中文正常的顯示出來。其實在第一段要取得信件裡的內容時,就遇到中文編碼的問題。

demo code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import imaplib,email,re
from email.header import decode_header
from bs4 import BeautifulSoup
from wordpress_xmlrpc import Client,WordPressPost,WordPressPost,WordPressTerm
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc.methods.posts import NewPost,EditPost
from wordpress_xmlrpc.methods.users import GetUserInfo

# 這樣子可以傳回中文字
def decode_subject(subject,encoding):
if encoding == None:
return subject
else:
return subject.decode(encoding)
# 用意跟上面的一樣, 只是為了處理編碼的問題
def showmessage(mail):
if mail.is_multipart():
for part in mail.get_payload():
showmessage(part)
else:
type = mail.get_content_charset()
if type == None:
return mail.get_payload()
else:
try:
return mail.get_payload(decode=1).decode(type)
except UnicodeDecodeError:
return mail

conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login('account','password)
conn.select("inbox") # connect to inbox.
typ, data = conn.search(None, 'ALL')
try:
for num in data[0].split()[-1:]: #從最新的信件開始讀起
typ, msg_data = conn.fetch(num, '(RFC822)')
for response_part in msg_data:
if isinstance(response_part, tuple):
msg = email.message_from_string(response_part[1].decode())
subject,encoding = decode_header(msg['subject'])[0]
mailsubject = decode_subject(subject,encoding)
#_from = msg['from']
body = showmessage(msg) # 編碼轉換
# 分析並將轉錄到wordpress上
for link in soup.find_all("table")[8:]:#start with 8th table element content = link.encode('big5').decode('cp950')
if content.find('class="contentword"') > -1:
news = BeautifulSoup(content)
for part in news.find_all('tr'):
td = part.find_all("td")
title = td[0].get_text()
puretext = re.sub('(\xa0)+',' ',td[1].get_text())
if title == '公布日期':
publishdate = puretext
elif title=='標題':
posttitle = puretext
elif title=='詳細內容':
postcontent = puretext
postToWP.post(posttitle,postcontent,"post tag")
move_mail(conn,num,'Archive.Today')
finally:
try:
conn.close()
except:
pass
conn.logout()

在3.3裡, email.message_from_string 傳進去的文字如果不加decode,會傳錯誤訊息出來。

1
email.message_from_string(response_part[1].decode())

發布文章到wordpress 參考網址: http://python-wordpress-xmlrpc.readthedocs.org/en/latest/index.html

class postToWP:
    def post(title,content,tag):
        client = Client('http://blog url//xmlrpc.php', 'username', 'password')        
        post = WordPressPost()
        post.title = title
        post.content = content
        post.terms_names = {
        'post_tag': [tag],              # 標籤(Tag)
        'category': [tag],              # 分類(Categories)
        }      
        post.post_status = 'publish'    # 如果沒有這一個,就會是草稿狀態
        post.id = client.call(NewPost(post))

#Grunt Need: Node.js installed

Install Steps

  1. Installing the CLI run command
1
npm install -g grunt-cli
  1. edit package.json and Gruntfile.js
1
2
3
4
5
6
{
"devDependencies": {
"grunt": "0.4.1",
"grunt-ts": "latest"
}
}

run npm install in command

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module.exports = function (grunt) {
// load the task
grunt.loadNpmTasks('grunt-ts');

// Configure grunt here

grunt.initConfig({
ts: {
dev: {
// a particular target
src: ['app/**/*.ts'], // The source typescript files, http://gruntjs.com/configuring-tasks#files
html: ['app/**/*.html'], // The source html files, https://github.com/basarat/grunt-ts#html-2-typescript-support
reference: './reference.ts', // If specified, generate this file that you can use for your reference management
out: 'app/app.js', // If specified, generate an out.js file which is the merged js file
watch: 'app',
},
},
});
grunt.registerTask('default', ['ts:dev']);
};

run grunt in command

這樣子 grunt 就會監控 在 app 資料夾下的 ts 檔案異動,並同時做 complie 的動作,如果有錯誤,會產生 complie error 的訊息。

In projectname.csproj 新增

 <PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <TypeScriptTarget>ES3</TypeScriptTarget>
    <TypeScriptIncludeComments>true</TypeScriptIncludeComments>
    <TypeScriptSourceMap>true</TypeScriptSourceMap>
    <TypeScriptModuleKind>AMD</TypeScriptModuleKind>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <TypeScriptTarget>ES3</TypeScriptTarget>
    <TypeScriptIncludeComments>false</TypeScriptIncludeComments>
    <TypeScriptSourceMap>false</TypeScriptSourceMap>
    <TypeScriptModuleKind>AMD</TypeScriptModuleKind>
  </PropertyGroup>
  <Import Project="$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets" />

在重新載入專案